package com.aids_malaria.ui.datacollect;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.aids_malaria.R;
import com.aids_malaria.app.App;
import com.aids_malaria.app.Constant;
import com.aids_malaria.base.BaseActivity;
import com.aids_malaria.db.DBManager;
import com.aids_malaria.event.Events;
import com.aids_malaria.helper.PointLengthFilter;
import com.aids_malaria.http.BaseObserver;
import com.aids_malaria.http.RetrofitFactory;
import com.aids_malaria.http.RxScheduler;
import com.aids_malaria.http.api.RetrofitApi;
import com.aids_malaria.http.request.AdverseReaction;
import com.aids_malaria.http.request.ConSymptom;
import com.aids_malaria.http.request.DataCollectRequest;
import com.aids_malaria.http.request.DataCollectType;
import com.aids_malaria.http.request.MicroResults;
import com.aids_malaria.http.request.OutdoorSprayAgent;
import com.aids_malaria.http.request.Symptom;
import com.aids_malaria.http.request.TokenDrug;
import com.aids_malaria.http.response.AreaC;
import com.aids_malaria.http.response.AreaV;
import com.aids_malaria.http.response.DiagnosisInfo;
import com.aids_malaria.http.response.DiagnosisInfoPack;
import com.aids_malaria.http.response.DiagnosisTreatmentInfo;
import com.aids_malaria.http.response.DrugallergyInfo;
import com.aids_malaria.http.response.DrugallergyInfoPack;
import com.aids_malaria.http.response.EaProcess;
import com.aids_malaria.http.response.EaProcessPack;
import com.aids_malaria.http.response.GetMedicalTreatment;
import com.aids_malaria.http.response.GetMedicalTreatmentPack;
import com.aids_malaria.http.response.PadMaxIdentitySeq;
import com.aids_malaria.http.response.PersonData;
import com.aids_malaria.http.response.PremedicalTreatment;
import com.aids_malaria.http.response.PremedicalTreatmentPack;
import com.aids_malaria.idcardcamera.camera.IDCardCamera;
import com.aids_malaria.util.BitmapUtil;
import com.aids_malaria.util.DateUtil;
import com.aids_malaria.util.DisplayUtil;
import com.aids_malaria.util.GsonUtil;
import com.aids_malaria.util.LogUtil;
import com.aids_malaria.util.ResourceUtil;
import com.aids_malaria.util.SpUtil;
import com.aids_malaria.util.StringUtil;
import com.aids_malaria.util.ToastUtil;
import com.aids_malaria.widget.dialog.ConfirmDialog;
import com.aids_malaria.widget.dialog.HeadSelectDialog;
import com.aids_malaria.widget.dialog.NumDialog;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.rxbus2.Subscribe;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class DataCollectActivity extends BaseActivity {

    public static final String WAY_COLLECT = "way_collect";
    public static final int WAY_BASE = 0;
    public static final int WAY_HEAL = 1;
    public static final int WAY_HISTORY = 2;
    public static final int WAY_MEDICINE_SECURE = 3;

    public static final String BUSY_TYPE = "busy_type";
    private int busy_type = 1;  // 1插入 2更改 3 删除

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ll_base_data)
    LinearLayout ll_base_data;
    @BindView(R.id.iv_base_data)
    ImageView iv_base_data;
    @BindView(R.id.ll_sick_heal)
    LinearLayout ll_sick_heal;
    @BindView(R.id.iv_sick_heal)
    ImageView iv_sick_heal;
    @BindView(R.id.iv_investigate_time)
    ImageView iv_investigate_time;
    @BindView(R.id.iv_clinical_symptoms)
    ImageView iv_clinical_symptoms;
    @BindView(R.id.ll_clinical_symptoms)
    LinearLayout ll_clinical_symptoms;
    @BindView(R.id.iv_test_result)
    ImageView iv_test_result;
    @BindView(R.id.ll_test_result)
    LinearLayout ll_test_result;
    @BindView(R.id.ll_sick_source)
    LinearLayout ll_sick_source;
    @BindView(R.id.ll_sick_handle)
    LinearLayout ll_sick_handle;
    @BindView(R.id.ll_medicine_heal)
    LinearLayout ll_medicine_heal;
    @BindView(R.id.iv_sick_source)
    ImageView iv_sick_source;
    @BindView(R.id.iv_sick_handle)
    ImageView iv_sick_handle;
    @BindView(R.id.iv_medicine_heal)
    ImageView iv_medicine_heal;
    @BindView(R.id.ll_history_sick)
    LinearLayout ll_history_sick;
    @BindView(R.id.iv_history_sick)
    ImageView iv_history_sick;
    @BindView(R.id.ll_medical_secure)
    LinearLayout ll_medical_secure;
    @BindView(R.id.iv_medical_secure)
    ImageView iv_medical_secure;

    // 基本数据view
    @BindView(R.id.iv_photo)
    ImageView iv_photo;
    @BindView(R.id.photo_bg)
    LinearLayout lv_photo;
    @BindView(R.id.camera_bg)
    ImageView camera_bg;
    @BindView(R.id.et_id)
    EditText et_id;
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.rg_sex)
    RadioGroup rg_sex;
    @BindView(R.id.rg_pregnant)
    RadioGroup rg_pregnant;
    @BindView(R.id.et_birthday)
    TextView et_birthday;
    @BindView(R.id.et_county)
    TextView et_county;
    @BindView(R.id.et_village)
    TextView et_village;
    @BindView(R.id.et_address_detail)
    EditText et_address_detail;
    @BindView(R.id.et_county2)
    TextView et_county2;
    @BindView(R.id.et_village2)
    TextView et_village2;
    @BindView(R.id.et_birth_place_detail)
    EditText et_birth_place_detail;
    @BindView(R.id.et_id_num)
    EditText et_id_num;
    @BindView(R.id.et_father)
    TextView et_father;
    @BindView(R.id.et_mother)
    TextView et_mother;
    @BindView(R.id.et_mobile_phone)
    EditText et_mobile_phone;
    @BindView(R.id.et_g_phone)
    EditText et_g_phone;
    @BindView(R.id.et_investigate_man)
    EditText et_investigate_man;
    @BindView(R.id.et_investigate_time)
    TextView et_investigate_time;
    @BindView(R.id.rg_is_die)
    RadioGroup rg_is_die;
    @BindView(R.id.rb_not_die)
    RadioButton rb_not_die;
    @BindView(R.id.et_x)
    EditText et_x;
    @BindView(R.id.et_y)
    EditText et_y;
    @BindView(R.id.rb_male)
    RadioButton rb_male;
    @BindView(R.id.rb_female)
    RadioButton rb_female;
    @BindView(R.id.rb_not_pregnant)
    RadioButton rb_not_pregnant;
    @BindView(R.id.rb_less_3)
    RadioButton rb_less_3;
    @BindView(R.id.rb_more_3)
    RadioButton rb_more_3;
    @BindView(R.id.rb_yes_die)
    RadioButton rb_yes_die;


    //////既往病史和治疗情况view
    @BindView(R.id.et_history_num)
    TextView et_history_num;
    @BindView(R.id.et_history_sick_time)
    TextView et_history_sick_time;
    @BindView(R.id.cb_medicine1)
    CheckBox cb_medicine1;
    @BindView(R.id.cb_medicine2)
    CheckBox cb_medicine2;
    @BindView(R.id.cb_medicine3)
    CheckBox cb_medicine3;
    @BindView(R.id.cb_medicine4)
    CheckBox cb_medicine4;
    @BindView(R.id.cb_medicine5)
    CheckBox cb_medicine5;
    @BindView(R.id.cb_medicine6)
    CheckBox cb_medicine6;
    @BindView(R.id.cb_medicine7)
    CheckBox cb_medicine7;
    @BindView(R.id.cb_medicine8)
    CheckBox cb_medicine8;
    @BindView(R.id.cb_medicine9)
    CheckBox cb_medicine9;
    @BindView(R.id.cb_medicine10)
    CheckBox cb_medicine10;
    @BindView(R.id.cb_medicine_other)
    CheckBox cb_medicine_other;
    @BindView(R.id.et_medicine_other)
    EditText et_medicine_other;
    @BindView(R.id.et_medical_days)
    EditText et_medical_days;
    @BindView(R.id.rg_xg_heal)
    RadioGroup rg_xg_heal;
    @BindView(R.id.et_history_investigate)
    EditText et_history_investigate;
    @BindView(R.id.et_history_investigate_time)
    TextView et_history_investigate_time;
    @BindView(R.id.rb_xg_yes)
    RadioButton rb_xg_yes;
    @BindView(R.id.rb_xg_not)
    RadioButton rb_xg_not;

    // 药物安全性view
    @BindView(R.id.et_medical_num)
    TextView et_medical_num;
    @BindView(R.id.et_allergy_medicine)
    EditText et_allergy_medicine;
    @BindView(R.id.et_allergy_history)
    EditText et_allergy_history;
    @BindView(R.id.cb_eat_feel_down)
    CheckBox cb_eat_feel_down;
    @BindView(R.id.cb_nausea)
    CheckBox cb_nausea;
    @BindView(R.id.cb_vomit)
    CheckBox cb_vomit;
    @BindView(R.id.cb_diarrhea)
    CheckBox cb_diarrhea;
    @BindView(R.id.cb_heart_ele_abnormal)
    CheckBox cb_heart_ele_abnormal;
    @BindView(R.id.cb_hemolysis)
    CheckBox cb_hemolysis;
    @BindView(R.id.cb_medical_other)
    CheckBox cb_medical_other;
    @BindView(R.id.et_medical_other)
    EditText et_medical_other;
    @BindView(R.id.et_medical_investigate)
    EditText et_medical_investigate;
    @BindView(R.id.et_medical_investigate_time)
    TextView et_medical_investigate_time;

    /////诊断治疗view
    // 临床症状
    @BindView(R.id.ll_sick1)
    LinearLayout ll_sick1;
    @BindView(R.id.ll_sick2)
    LinearLayout ll_sick2;
    @BindView(R.id.et_heal_num)
    TextView et_heal_num;
    @BindView(R.id.et_diagnosis_man)
    EditText et_diagnosis_man;
    @BindView(R.id.et_diagnosis_time)
    TextView et_diagnosis_time;
    @BindView(R.id.rg_heal_result)
    RadioGroup rg_heal_result;
    @BindView(R.id.et_sick_upload_time)
    TextView et_sick_upload_time;
    @BindView(R.id.et_report_unit)
    EditText et_report_unit;
    @BindView(R.id.et_reporter)
    EditText et_reporter;
    @BindView(R.id.cb_temperature_normal)
    CheckBox cb_temperature_normal;
    @BindView(R.id.cb_last_hot)
    CheckBox cb_last_hot;
    @BindView(R.id.cb_other_day_hot)
    CheckBox cb_other_day_hot;
    @BindView(R.id.cb_hot_suddenly)
    CheckBox cb_hot_suddenly;
    @BindView(R.id.cb_cold)
    CheckBox cb_cold;
    @BindView(R.id.cb_sweat)
    CheckBox cb_sweat;
    @BindView(R.id.cb_headache)
    CheckBox cb_headache;
    @BindView(R.id.cb_diarrhea2)
    CheckBox cb_diarrhea2;
    @BindView(R.id.cb_muscle_ache)
    CheckBox cb_muscle_ache;
    @BindView(R.id.cb_shiver)
    CheckBox cb_shiver;
    @BindView(R.id.cb_twitch)
    CheckBox cb_twitch;
    @BindView(R.id.cb_shock)
    CheckBox cb_shock;
    @BindView(R.id.cb_oliguria)
    CheckBox cb_oliguria;
    @BindView(R.id.cb_hematuria)
    CheckBox cb_hematuria;
    @BindView(R.id.cb_other_symptoms)
    CheckBox cb_other_symptoms;
    @BindView(R.id.et_other_symptoms)
    EditText et_other_symptoms;
    @BindView(R.id.et_sick_address_county)
    TextView et_sick_address_county;
    @BindView(R.id.et_sick_address_village)
    TextView et_sick_address_village;
    @BindView(R.id.et_detail_address)
    EditText et_detail_address;
    @BindView(R.id.rb_noomal)
    RadioButton rb_noomal;
    @BindView(R.id.rb_uncertain)
    RadioButton rb_uncertain;
    @BindView(R.id.rb_certain)
    RadioButton rb_certain;

    //检验结果
    @BindView(R.id.cb_negative)
    CheckBox cb_negative;
    @BindView(R.id.cb_tertian_malaria)
    CheckBox cb_tertian_malaria;
    @BindView(R.id.cb_malignant_malaria)
    CheckBox cb_malignant_malaria;
    @BindView(R.id.cb_quartan_malaria)
    CheckBox cb_quartan_malaria;
    @BindView(R.id.cb_ovale_malaria)
    CheckBox cb_ovale_malaria;
    @BindView(R.id.cb_mixed_infection)
    CheckBox cb_mixed_infection;
    @BindView(R.id.et_protozoan_density)
    EditText et_protozoan_density;
    @BindView(R.id.et_gametophyte_carrying_rate)
    EditText et_gametophyte_carrying_rate;
    @BindView(R.id.et_ratio_of_male_to_female_gametophyte)
    EditText et_ratio_of_male_to_female_gametophyte;
    @BindView(R.id.et_mirror_man)
    EditText et_mirror_man;
    @BindView(R.id.et_mirror_time)
    TextView et_mirror_time;
    @BindView(R.id.cb_negative2)
    CheckBox cb_negative2;
    @BindView(R.id.cb_tertian_malaria2)
    CheckBox cb_tertian_malaria2;
    @BindView(R.id.cb_malignant_malaria2)
    CheckBox cb_malignant_malaria2;
    @BindView(R.id.cb_quartan_malaria2)
    CheckBox cb_quartan_malaria2;
    @BindView(R.id.cb_ovale_malaria2)
    CheckBox cb_ovale_malaria2;
    @BindView(R.id.cb_mixed_infection2)
    CheckBox cb_mixed_infection2;
    @BindView(R.id.et_checker)
    EditText et_checker;
    @BindView(R.id.et_check_time)
    TextView et_check_time;
    @BindView(R.id.ll_ex1)
    LinearLayout ll_ex1;
    @BindView(R.id.ll_ex2)
    LinearLayout ll_ex2;

    // 感染来源调查
    @BindView(R.id.rg_2week_sick)
    RadioGroup rg_2week_sick;
    @BindView(R.id.et_sick_address_county2)
    TextView et_sick_address_county2;
    @BindView(R.id.et_sick_address_village2)
    TextView et_sick_address_village2;
    @BindView(R.id.et_detail_address2)
    EditText et_detail_address2;
    @BindView(R.id.rg_2week_blood)
    RadioGroup rg_2week_blood;
    @BindView(R.id.rg_1week_family_hot)
    RadioGroup rg_1week_family_hot;
    @BindView(R.id.rg_sick_type)
    RadioGroup rg_sick_type;
    @BindView(R.id.et_checker_source)
    EditText et_checker_source;
    @BindView(R.id.et_check_time_source)
    EditText et_check_time_source;
    @BindView(R.id.rb_2week_yes)
    RadioButton rb_2week_yes;
    @BindView(R.id.rb_2week_not)
    RadioButton rb_2week_not;
    @BindView(R.id.rb_2week_blood_yes)
    RadioButton rb_2week_blood_yes;
    @BindView(R.id.rb_2week_blood_not)
    RadioButton rb_2week_blood_not;
    @BindView(R.id.rb_1week_family_hot_yes)
    RadioButton rb_1week_family_hot_yes;
    @BindView(R.id.rb_1week_family_hot_not)
    RadioButton rb_1week_family_hot_not;
    @BindView(R.id.rb_local_village_sick)
    RadioButton rb_local_village_sick;
    @BindView(R.id.rb_outside_village_sick)
    RadioButton rb_outside_village_sick;
    @BindView(R.id.rb_outside_sick)
    RadioButton rb_outside_sick;

    @BindView(R.id.ll_lx1)
    LinearLayout ll_lx1;
    @BindView(R.id.ll_lx2)
    LinearLayout ll_lx2;
    // 疫源处理
    @BindView(R.id.et_handle_num)
    TextView et_handle_num;
    @BindView(R.id.et_ps_time)
    TextView et_ps_time;
    @BindView(R.id.et_inside_ps_thing)
    EditText et_inside_ps_thing;
    @BindView(R.id.rg_outside_ps_thing)
    RadioGroup rg_outside_ps_thing;
    @BindView(R.id.rg_outside_ps_range)
    RadioGroup rg_outside_ps_range;
    @BindView(R.id.rg_mosquito_each_month)
    RadioGroup rg_mosquito_each_month;
    @BindView(R.id.et_handle_checker)
    EditText et_handle_checker;
    @BindView(R.id.rb_jjmdl)
    RadioButton rb_jjmdl;
    @BindView(R.id.rb_other_ps)
    RadioButton rb_other_ps;
    @BindView(R.id.et_other_ps)
    EditText et_other_ps;
    @BindView(R.id.rb_metre_50)
    RadioButton rb_metre_50;
    @BindView(R.id.rb_over_50m)
    RadioButton rb_over_50m;
    @BindView(R.id.rb_over_100m)
    RadioButton rb_over_100m;
    @BindView(R.id.rb_day_10)
    RadioButton rb_day_10;
    @BindView(R.id.rb_over_10day)
    RadioButton rb_over_10day;
    @BindView(R.id.rb_over_20day)
    RadioButton rb_over_20day;

    // 药物治疗情况
    @BindView(R.id.et_heal_sit_num)
    TextView et_heal_sit_num;
    @BindView(R.id.et_eat_medicine_time)
    TextView et_eat_medicine_time;
    @BindView(R.id.cb2_medicine1)
    CheckBox cb2_medicine1;
    @BindView(R.id.cb2_medicine2)
    CheckBox cb2_medicine2;
    @BindView(R.id.cb2_medicine3)
    CheckBox cb2_medicine3;
    @BindView(R.id.cb2_medicine4)
    CheckBox cb2_medicine4;
    @BindView(R.id.cb2_medicine5)
    CheckBox cb2_medicine5;
    @BindView(R.id.cb2_medicine6)
    CheckBox cb2_medicine6;
    @BindView(R.id.cb2_medicine7)
    CheckBox cb2_medicine7;
    @BindView(R.id.cb2_medicine8)
    CheckBox cb2_medicine8;
    @BindView(R.id.cb2_medicine9)
    CheckBox cb2_medicine9;
    @BindView(R.id.cb2_medicine10)
    CheckBox cb2_medicine10;
    @BindView(R.id.cb2_medicine_other)
    CheckBox cb2_medicine_other;
    @BindView(R.id.et2_medicine_other)
    EditText et2_medicine_other;
    @BindView(R.id.rg_heal_in_hospital)
    RadioGroup rg_heal_in_hospital;
    @BindView(R.id.et_hospital_num)
    EditText et_hospital_num;
    @BindView(R.id.et_hospital_time)
    TextView et_hospital_time;
    @BindView(R.id.et_hospital_time2)
    TextView et_hospital_time2;
    @BindView(R.id.rg_get_medicine_way)
    RadioGroup rg_get_medicine_way;
    @BindView(R.id.et_heal_sit_checker)
    EditText et_heal_sit_checker;
    @BindView(R.id.et_other_way)
    EditText et_other_way;
    @BindView(R.id.rb_heal_in_hospital_yes)
    RadioButton rb_heal_in_hospital_yes;
    @BindView(R.id.rb_heal_in_hospital_not)
    RadioButton rb_heal_in_hospital_not;
    @BindView(R.id.rb_send_medicine)
    RadioButton rb_send_medicine;
    @BindView(R.id.rb_getmedicine_by_hospital)
    RadioButton rb_getmedicine_by_hospital;
    @BindView(R.id.rb_other_way)
    RadioButton rb_other_way;
    @BindView(R.id.ll_in_hospital)
    LinearLayout ll_in_hospital;
    @BindView(R.id.ll_in_hospital_time)
    LinearLayout ll_in_hospital_time;

    // 基本数据
    private int sex = 1; // 性别，默认男
    private int pregnant = 0;
    private int die = 0;
    private String photo = "";
    private String current_county_code = "";
    private String current_village_code = "";
    private String birth_county_code = "";
    private String birth_village_code = "";
    private String current_county_name = "";
    private String birth_county_name = "";
    private String father_code = "";
    private String mother_code = "";

    private String sick_place_county_code = ""; // 发病地点code
    private String sick_place_village_code = "";
    private String sick_place_county_name = "";

    private String sick_place_county_code2 = ""; // 流行区地点code
    private String sick_place_village_code2 = "";
    private String sick_place_county_name2 = "";

    private int is_xg = 1; // 是否修根治疗
    private int is_role = 0; // 发病前2周内是否有其他流行区居留史
    private int is_transfusion_twoweek = 1; // 发病前2周内是否有输血史
    private int is_visitor_fever = 1; // 近1月内家庭成员或来访亲友是否有人发热
    private int infection_source_type = 1; // 感染来源分类
    private int out_range = 1; // 室外喷洒范围
    private int bednets_mu_rate = 1; // 每月使用蚊帐率
    private int is_hospital = 1; // 是否住院
    private int medicint_access = 1; // 获取药物方式
    private int heal_result = 0;// 诊断结果

    private int diagnosis_busy_type = 1; // 诊疗type
    private int ea_busy_type = 1; // 疫源处理type
    private int medical_busy_type = 1; // 药物治疗情况type
    private int premedical_busy_type = 1; // 既往病史type
    private int drugallergy_busy_type = 1; // 药物安全性type
    private int person_busy_type = 1; // 人员基础数据type

    private HeadSelectDialog mHeadSelectDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_data_collect;
    }

    @Override
    protected void initView() {
        super.initView();
        tv_title.setText(getString(R.string.data_get1));

        PointLengthFilter pointLengthFilter = new PointLengthFilter(6);
        et_x.setFilters(new InputFilter[]{pointLengthFilter});
        et_y.setFilters(new InputFilter[]{pointLengthFilter});
        et_x.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        et_y.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
    }

    @Override
    protected void initData() {
        super.initData();
        int intExtra = getIntent().getIntExtra(WAY_COLLECT, -1);
        busy_type = getIntent().getIntExtra(BUSY_TYPE,1);
        switch (intExtra){
            case WAY_BASE:
                baseDataSwitch();
                break;
            case WAY_HEAL:
                sickHealSwitch();
                break;
            case WAY_HISTORY:
                historySickSwitch();
                break;
            case WAY_MEDICINE_SECURE:
                medicalSecureSwitch();
                break;
        }
        et_investigate_man.setText(SpUtil.getString(mContext, Constant.USER_ACCOUNT, "")); // 默认获取当前登录人员
        String identity_code = getIntent().getStringExtra("identity_code");
        if (!TextUtils.isEmpty(identity_code)){
            et_id_num.setText(identity_code);
            GetDiagnosisTreatmentInfo(identity_code);
        }
        initCbListener();
        initEtListener();
        String format = DateUtil.getFullTimeFormatter().format(new Date());
        et_investigate_time.setText(format);
        et_check_time_source.setText(format);
        et_history_investigate_time.setText(format);
        et_medical_investigate_time.setText(format);
        initRgListener();
    }

    private void initCbListener(){
        cb_malignant_malaria.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    ll_ex1.setVisibility(View.VISIBLE);
                    ll_ex2.setVisibility(View.VISIBLE);
                }else {
                    ll_ex1.setVisibility(View.GONE);
                    ll_ex2.setVisibility(View.GONE);
                }
            }
        });
        rb_other_ps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                if (b){
                    et_other_ps.setEnabled(true);
                }else {
                    et_other_ps.setText("");
                    et_other_ps.setEnabled(false);
                }
            }
        });
        cb2_medicine_other.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    et2_medicine_other.setEnabled(true);
                }else {
                    et2_medicine_other.setText("");
                    et2_medicine_other.setEnabled(false);
                }
            }
        });
        cb_medicine_other.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    et_medicine_other.setEnabled(true);
                }else {
                    et_medicine_other.setText("");
                    et_medicine_other.setEnabled(false);
                }
            }
        });
        cb_medical_other.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    et_medical_other.setEnabled(true);
                }else {
                    et_medical_other.setText("");
                    et_medical_other.setEnabled(false);
                }
            }
        });
        cb_other_symptoms.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    et_other_symptoms.setEnabled(true);
                }else {
                    et_other_symptoms.setText("");
                    et_other_symptoms.setEnabled(false);
                }
            }
        });
        rb_other_way.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                if (b){
                    et_other_way.setEnabled(true);
                }else {
                    et_other_way.setText("");
                    et_other_way.setEnabled(false);
                }
            }
        });

    }

    private void initEtListener(){
        et_checker_source.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    et_checker_source.setText(et_checker.getText().toString().trim());
                }
            }
        });
        et_handle_checker.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    et_handle_checker.setText(et_checker_source.getText().toString().trim());
                }
            }
        });
        et_heal_sit_checker.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    et_heal_sit_checker.setText(et_handle_checker.getText().toString().trim());
                }
            }
        });
        et_history_investigate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    et_history_investigate.setText(et_heal_sit_checker.getText().toString().trim());
                }
            }
        });
        et_medical_investigate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    et_medical_investigate.setText(et_history_investigate.getText().toString());
                }
            }
        });
    }

    private void initRgListener(){
        rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_male:
                        sex = 1;
                        break;
                    case R.id.rb_female:
                        sex = 2;
                        break;
                }
            }
        });
        rg_pregnant.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_not_pregnant:
                        pregnant = 0;
                        break;
                    case R.id.rb_less_3:
                        pregnant = 1;
                        break;
                    case R.id.rb_more_3:
                        pregnant = 2;
                        break;
                }
            }
        });
        rg_is_die.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_yes_die:
                        ConfirmDialog.newInstance(true, getString(R.string.hint), getString(R.string.hint_is_die))
                                .setCancelConfirOption(new ConfirmDialog.CancelConfirOption() {
                                    @Override
                                    public void cancel() {
                                        die = 0;
                                        rb_not_die.setChecked(true);
                                    }

                                    @Override
                                    public void confirm() {
                                        die = 1;
                                    }
                                })
                                .setOutCancel(true)
                                .setWidth(DisplayUtil.px2dp((int) (DisplayUtil.getWindowWidth() * 0.5)))
                                .show(getSupportFragmentManager());

                        break;
                    case R.id.rb_not_die:
                        die = 0;
                        break;
                }
            }
        });

        rg_heal_result.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_noomal:
                        ll_sick1.setVisibility(View.GONE);
                        ll_sick2.setVisibility(View.GONE);
                        ll_in_hospital.setVisibility(View.GONE);
                        ll_in_hospital_time.setVisibility(View.GONE);
                        heal_result = 0; // 正常
                        break;
                    case R.id.rb_uncertain:
                        heal_result = 1; // 疑似
                        ll_sick1.setVisibility(View.VISIBLE);
                        ll_sick2.setVisibility(View.VISIBLE);
                        ll_in_hospital.setVisibility(View.VISIBLE);
                        ll_in_hospital_time.setVisibility(View.VISIBLE);
                        break;
                    case R.id.rb_certain:
                        heal_result = 2; //确诊病例
                        ll_sick1.setVisibility(View.VISIBLE);
                        ll_sick2.setVisibility(View.VISIBLE);
                        ll_in_hospital.setVisibility(View.VISIBLE);
                        ll_in_hospital_time.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });
        rg_xg_heal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_xg_yes:
                        is_xg = 1;
                        break;
                    case R.id.rb_xg_not:
                        is_xg = 0;
                        break;
                }
            }
        });

        rg_2week_sick.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_2week_yes:
                        is_role = 1;
                        ll_lx1.setVisibility(View.VISIBLE);
                        ll_lx2.setVisibility(View.VISIBLE);
                        break;
                    case R.id.rb_2week_not:
                        is_role = 0;
                        ll_lx1.setVisibility(View.GONE);
                        ll_lx2.setVisibility(View.GONE);
                        break;
                }
            }
        });

        rg_2week_blood.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_2week_blood_yes:
                        is_transfusion_twoweek = 1;
                        break;
                    case R.id.rb_2week_blood_not:
                        is_transfusion_twoweek = 0;
                        break;
                }
            }
        });

        rg_1week_family_hot.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_1week_family_hot_yes:
                        is_visitor_fever = 1;
                        break;
                    case R.id.rb_1week_family_hot_not:
                        is_visitor_fever = 0;
                        break;
                }
            }
        });

        rg_sick_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_local_village_sick:
                        infection_source_type = 1;
                        break;
                    case R.id.rb_outside_village_sick:
                        infection_source_type = 2;
                        break;
                    case R.id.rb_outside_sick:
                        infection_source_type = 4;
                        break;
                }
            }
        });

        rg_outside_ps_range.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_metre_50:
                        out_range = 1;
                        break;
                    case R.id.rb_over_50m:
                        out_range = 2;
                        break;
                    case R.id.rb_over_100m:
                        out_range = 3;
                        break;
                }
            }
        });

        rg_mosquito_each_month.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_day_10:
                        bednets_mu_rate = 1;
                        break;
                    case R.id.rb_over_10day:
                        bednets_mu_rate = 2;
                        break;
                    case R.id.rb_over_20day:
                        bednets_mu_rate = 3;
                        break;
                }
            }
        });

        rg_heal_in_hospital.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_heal_in_hospital_yes:
                        is_hospital = 1;
                        et_hospital_num.setEnabled(true);
                        break;
                    case R.id.rb_heal_in_hospital_not:
                        is_hospital = 0;
                        et_hospital_num.setEnabled(false);
                        et_hospital_num.setText("");
                        break;
                }
            }
        });

        rg_get_medicine_way.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_send_medicine:
                        medicint_access = 1;
                        break;
                    case R.id.rb_getmedicine_by_hospital:
                        medicint_access = 2;
                        break;
                    case R.id.rb_other_way:
                        medicint_access = 3;
                        break;
                }
            }
        });
    }

    private void baseDataSwitch(){
        if (ll_base_data.getVisibility() == View.VISIBLE){
            ll_base_data.setVisibility(View.GONE);
            iv_base_data.setImageResource(R.mipmap.add);
        }else {
            ll_base_data.setVisibility(View.VISIBLE);
            iv_base_data.setImageResource(R.mipmap.subtract);
        }
    }

    private void sickHealSwitch(){
        if (ll_sick_heal.getVisibility() == View.VISIBLE){
            ll_sick_heal.setVisibility(View.GONE);
            iv_sick_heal.setImageResource(R.mipmap.add);
        }else {
            ll_sick_heal.setVisibility(View.VISIBLE);
            iv_sick_heal.setImageResource(R.mipmap.subtract);
        }
    }

    private void testResultSwitch(){
        if (ll_test_result.getVisibility() == View.VISIBLE){
            ll_test_result.setVisibility(View.GONE);
            iv_test_result.setImageResource(R.mipmap.add);
        }else {
            ll_test_result.setVisibility(View.VISIBLE);
            iv_test_result.setImageResource(R.mipmap.subtract);
        }
    }

    private void clinicalSymptomsSwitch(){
        if(ll_clinical_symptoms.getVisibility() == View.VISIBLE){
            ll_clinical_symptoms.setVisibility(View.GONE);
            iv_clinical_symptoms.setImageResource(R.mipmap.add);
        }else {
            ll_clinical_symptoms.setVisibility(View.VISIBLE);
            iv_clinical_symptoms.setImageResource(R.mipmap.subtract);
        }
    }

    private void sickSourceSwitch(){
        if(ll_sick_source.getVisibility() == View.VISIBLE){
            ll_sick_source.setVisibility(View.GONE);
            iv_sick_source.setImageResource(R.mipmap.add);
        }else {
            ll_sick_source.setVisibility(View.VISIBLE);
            iv_sick_source.setImageResource(R.mipmap.subtract);
        }
    }

    private void sickHandleSwitch(){
        if(ll_sick_handle.getVisibility() == View.VISIBLE){
            ll_sick_handle.setVisibility(View.GONE);
            iv_sick_handle.setImageResource(R.mipmap.add);
        }else {
            ll_sick_handle.setVisibility(View.VISIBLE);
            iv_sick_handle.setImageResource(R.mipmap.subtract);
        }
    }

    private void medicineHealSwitch(){
        if(ll_medicine_heal.getVisibility() == View.VISIBLE){
            ll_medicine_heal.setVisibility(View.GONE);
            iv_medicine_heal.setImageResource(R.mipmap.add);
        }else {
            ll_medicine_heal.setVisibility(View.VISIBLE);
            iv_medicine_heal.setImageResource(R.mipmap.subtract);
        }
    }

    private void historySickSwitch(){
        if(ll_history_sick.getVisibility() == View.VISIBLE){
            ll_history_sick.setVisibility(View.GONE);
            iv_history_sick.setImageResource(R.mipmap.add);
        }else {
            ll_history_sick.setVisibility(View.VISIBLE);
            iv_history_sick.setImageResource(R.mipmap.subtract);
        }
    }

    private void medicalSecureSwitch(){
        if(ll_medical_secure.getVisibility() == View.VISIBLE){
            ll_medical_secure.setVisibility(View.GONE);
            iv_medical_secure.setImageResource(R.mipmap.add);
        }else {
            ll_medical_secure.setVisibility(View.VISIBLE);
            iv_medical_secure.setImageResource(R.mipmap.subtract);
        }
    }

    /**
     * 序号加
     * @param editText
     */
    private void numAdd(TextView editText){
        String strNum = editText.getText().toString().trim();
        if (TextUtils.isEmpty(strNum)){
            return;
        }
        try{
            int num = Integer.parseInt(strNum) + 1;
//            if (num <= 5){
                editText.setText("" + num);
//            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 序号选择 1 - 5
     * @param editText
     */
    private void numSelect(TextView editText,int dataSize, int type){
        new NumDialog(mContext,dataSize, new NumDialog.OnNumClickListener() {
            @Override
            public void onNumClick(int num, Dialog dialog) {
                editText.setText(num + "");
                dialog.dismiss();
                switch (type){
                    case 1:
                        //诊断治疗
                        ll_medicine_heal.setVisibility(View.GONE); // 收缩药物治疗情况
                        iv_medicine_heal.setImageResource(R.mipmap.add);
                        //诊疗数据
                        showDiagnosisInfo(Integer.parseInt(et_heal_num.getText().toString()));
                        //疫源处理
                        int EaaxNum = 0;
                        for (int i = 0; i <ea_processs.size() ; i++) {
                            EaProcessPack eaProcessPack = ea_processs.get(i);
                            if (eaProcessPack.getRecord_no() > EaaxNum){
                                EaaxNum = eaProcessPack.getRecord_no();
                            }
                        }
                        et_handle_num.setText(EaaxNum>0?EaaxNum + "" :"");
//                        String handnum = et_handle_num.getText().toString().trim();
                        showEaProcessInfo(EaaxNum);

                        //药物治疗
//                        String healsitnum = et_heal_sit_num.getText().toString().trim();
                        int getMedicalmaxNum = 0;
                        for (int i = 0; i <meical_treatments.size() ; i++) {
                            if (!StringUtil.isEmpty(et_heal_num.getText().toString()) && meical_treatments.get(i).getRecord_no() == Integer.parseInt(et_heal_num.getText().toString())){
                                GetMedicalTreatmentPack medicalTreatmentPack = meical_treatments.get(i);
                                getMedicalmaxNum = Math.max(medicalTreatmentPack.getRecord_seq(),getMedicalmaxNum);
                            }
                        }
                        et_heal_sit_num.setText(getMedicalmaxNum> 0 ? getMedicalmaxNum + "" : "");
                        showGetMedicalTreatmentInfo(getMedicalmaxNum,false);
                        break;
                    case 2:
                        showEaProcessInfo(Integer.parseInt(et_handle_num.getText().toString()));
                        break;
                    case 3:
//                        ll_medicine_heal.setVisibility(View.GONE); // 收缩药物治疗情况
//                        iv_medicine_heal.setImageResource(R.mipmap.add);
                        showGetMedicalTreatmentInfo(Integer.parseInt(et_heal_sit_num.getText().toString()),false);
                        break;
                    case 4:
                        showPremedicalTreatmentInfo(Integer.parseInt(et_history_num.getText().toString()));
                        break;
                    case 5:
                        showDrugallergyData(Integer.parseInt(et_medical_num.getText().toString()));
                        break;
                }
            }
        }).show();
    }

    private int getMaxmeical_treatmentsNum(){
        int maxNum = 0;
        for (int i = 0; i <meical_treatments.size() ; i++) {
            if (!StringUtil.isEmpty(et_heal_num.getText().toString()) &&
                    meical_treatments.get(i).getRecord_no() == Integer.parseInt(et_heal_num.getText().toString())){
                GetMedicalTreatmentPack medicalTreatmentPack = meical_treatments.get(i);
                maxNum = Math.max(medicalTreatmentPack.getRecord_seq(),maxNum);
            }
        }
        return maxNum;
    }

    private int getMaxea_processsNum(){
        int maxNum1 = 0;
        for (int i = 0; i <ea_processs.size() ; i++) {
            EaProcessPack eaProcessPack = ea_processs.get(i);
            if (!StringUtil.isEmpty(et_heal_num.getText().toString()) &&
                    ea_processs.get(i).getRecord_no() == Integer.parseInt(et_heal_num.getText().toString())){
                maxNum1 = Math.max(ea_processs.get(i).getRecord_seq(),maxNum1);
            }
        }
        return maxNum1;
    }

    //药物安全性
    private int getMaxdrugallergy_infosNum(){
        int maxNum2 = 0;
        for (int i = 0; i <drugallergy_infos.size() ; i++) {
            DrugallergyInfoPack eaProcessPack = drugallergy_infos.get(i);
            maxNum2 = Math.max(drugallergy_infos.get(i).getRecord_seq(), maxNum2);
        }
        return maxNum2;
    }

    //获取既往病史最大序号
    private int getMaxPreNum(){
        int maxNum3 = 0;
        for (int i = 0; i <premedical_treatments.size() ; i++) {
            PremedicalTreatmentPack eaProcessPack = premedical_treatments.get(i);
            maxNum3 = Math.max(eaProcessPack.getRecord_seq(), maxNum3);
        }
        return maxNum3;
    }

    //诊疗数据
    private int getMaxdiagnosis_infosNum(){
        int maxNum4 = 0;
        for (int i = 0; i <diagnosis_infos.size() ; i++) {
            DiagnosisInfoPack eaProcessPack = diagnosis_infos.get(i);
            maxNum4 = Math.max(eaProcessPack.getRecord_no(),maxNum4);
        }
        return maxNum4;
    }

    @OnClick({R.id.ll_back,R.id.iv_photo,R.id.photo_bg,R.id.ll_base_data_switch,R.id.ll_sick_heal_switch,R.id.ll_test_result_switch,R.id.ll_clinical_symptoms_switch
    ,R.id.ll_sick_source_switch,R.id.ll_sick_handle_switch,R.id.ll_medicine_heal_switch,R.id.ll_history_sick_switch
    ,R.id.ll_medical_secure_switch,R.id.tv_gain_id,R.id.iv_father_select,R.id.iv_mother_select,R.id.tv_gain_xy,R.id.tv_commit_base
    ,R.id.tv_commit_history,R.id.tv_medical_commit,R.id.tv_commit_heal_sit,R.id.iv_birthday
    ,R.id.iv_diagnosis_time_select,R.id.iv_sick_upload_time,R.id.iv_mirror_time,R.id.iv_check_time,R.id.iv_check_time_source
    ,R.id.iv_eat_medicine_time,R.id.iv_hospital_time,R.id.iv_hospital_time2,R.id.iv_history_time_select,R.id.iv_medical_investigate_time
    ,R.id.iv_current_county_select,R.id.iv_current_village_select,R.id.iv_birth_county_select,R.id.iv_birth_village_select
    ,R.id.iv_sick_place_county_select,R.id.iv_sick_place_village_select,R.id.iv_sick_address_county2,R.id.iv_sick_address_village2
    ,R.id.iv_history_sick_time,R.id.iv_ps_time,R.id.iv_heal_num_add,R.id.iv_heal_num_sub,R.id.iv_history_num_add,R.id.iv_history_num_sub
    ,R.id.iv_medical_num_add,R.id.iv_medical_num_sub,R.id.iv_handle_num_add,R.id.iv_handle_num_sub,R.id.iv_heal_sit_num_add
    ,R.id.iv_heal_sit_num_sub,R.id.iv_id_select,R.id.et_birthday,R.id.et_county,R.id.et_village,R.id.et_county2,R.id.et_village2,R.id.et_heal_num
    ,R.id.et_diagnosis_time,R.id.et_history_num,R.id.et_history_sick_time,R.id.et_history_investigate_time,R.id.et_medical_num
    ,R.id.et_medical_investigate_time,R.id.et_heal_sit_num,R.id.et_investigate_time,R.id.iv_investigate_time,R.id.et_father,R.id.et_mother
    ,R.id.et_sick_upload_time,R.id.et_eat_medicine_time,R.id.et_hospital_time,R.id.et_hospital_time2
    ,R.id.et_sick_address_county2,R.id.et_sick_address_village2,R.id.et_mirror_time,R.id.et_check_time,R.id.et_handle_num,R.id.et_ps_time
    ,R.id.et_sick_address_county,R.id.et_sick_address_village})
    public void onClick(View view){
        Bundle bundle = null;
        switch (view.getId()){
            case R.id.iv_heal_sit_num_add:
                numAdd(et_heal_sit_num);
                if (!TextUtils.isEmpty(et_heal_sit_num.getText().toString())){
                    showGetMedicalTreatmentInfo(Integer.parseInt(et_heal_sit_num.getText().toString()),false);
                }
                break;
            case R.id.iv_heal_sit_num_sub:
            case R.id.et_heal_sit_num:
                //药物治疗情况

                numSelect(et_heal_sit_num,getMaxmeical_treatmentsNum(),3);
                break;
            case R.id.iv_handle_num_add:
                numAdd(et_handle_num);
                if (!TextUtils.isEmpty(et_handle_num.getText().toString())){
                    showEaProcessInfo(Integer.parseInt(et_handle_num.getText().toString()));
                }
                break;
            case R.id.iv_handle_num_sub:
            case R.id.et_handle_num:
                numSelect(et_handle_num,getMaxea_processsNum(),2);
                break;
            case R.id.iv_medical_num_add:
                numAdd(et_medical_num);
                if (!TextUtils.isEmpty(et_medical_num.getText().toString())){
                    showDrugallergyData(Integer.parseInt(et_medical_num.getText().toString()));
                }
                break;
            case R.id.iv_medical_num_sub:
            case R.id.et_medical_num:
                numSelect(et_medical_num,getMaxdrugallergy_infosNum(),5);
                break;
            case R.id.iv_history_num_add:
                numAdd(et_history_num);
                if (!TextUtils.isEmpty(et_history_num.getText().toString())){
                    showPremedicalTreatmentInfo(Integer.parseInt(et_history_num.getText().toString()));
                }
                break;
            case R.id.iv_history_num_sub:
            case R.id.et_history_num:
                numSelect(et_history_num,getMaxPreNum(),4);
                break;
            case R.id.iv_heal_num_add:
                numAdd(et_heal_num);
                if (!TextUtils.isEmpty(et_heal_num.getText().toString())){
//                    showDiagnosisInfo(Integer.parseInt(et_heal_num.getText().toString()));
//                    String handnum = et_handle_num.getText().toString().trim();
//                    showEaProcessInfo(TextUtils.isEmpty(handnum) ? -1 : Integer.parseInt(handnum));
//                    String healsitnum = et_heal_sit_num.getText().toString().trim();
//                    showGetMedicalTreatmentInfo(TextUtils.isEmpty(healsitnum) ? -1 : Integer.parseInt(healsitnum),false);
                    //诊断治疗
                    ll_medicine_heal.setVisibility(View.GONE); // 收缩药物治疗情况
                    iv_medicine_heal.setImageResource(R.mipmap.add);
                    //诊疗数据
                    showDiagnosisInfo(Integer.parseInt(et_heal_num.getText().toString()));
                    //疫源处理
                    int EaaxNum = 0;
                    for (int i = 0; i <ea_processs.size() ; i++) {
                        EaProcessPack eaProcessPack = ea_processs.get(i);
                        if (eaProcessPack.getRecord_no() > EaaxNum){
                            EaaxNum = eaProcessPack.getRecord_no();
                        }
                    }
                    et_handle_num.setText(EaaxNum>0?EaaxNum + "" :"");
//                        String handnum = et_handle_num.getText().toString().trim();
                    showEaProcessInfo(EaaxNum);

                    //药物治疗
//                        String healsitnum = et_heal_sit_num.getText().toString().trim();
                    int getMedicalmaxNum = 0;
                    for (int i = 0; i <meical_treatments.size() ; i++) {
                        if (!StringUtil.isEmpty(et_heal_num.getText().toString()) && meical_treatments.get(i).getRecord_no() == Integer.parseInt(et_heal_num.getText().toString())){
                            GetMedicalTreatmentPack medicalTreatmentPack = meical_treatments.get(i);
                            getMedicalmaxNum = Math.max(medicalTreatmentPack.getRecord_seq(),getMedicalmaxNum);
                        }
                    }
                    et_heal_sit_num.setText(getMedicalmaxNum> 0 ? getMedicalmaxNum + "" : "");
                    showGetMedicalTreatmentInfo(getMedicalmaxNum,false);
                }
                break;
            case R.id.iv_heal_num_sub:
            case R.id.et_heal_num:
                numSelect(et_heal_num,getMaxdiagnosis_infosNum(),1);
                break;
            case R.id.iv_ps_time:
            case R.id.et_ps_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_ps_time.setText(DateUtil.getYMDTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.ll_back:
                finish();
                break;
            case R.id.photo_bg:
            case R.id.iv_photo:
                if(null == mHeadSelectDialog){
                    mHeadSelectDialog = new HeadSelectDialog(mContext, new HeadSelectDialog.OnHeadSelectFuncClickListener() {
                        @Override
                        public void takePhoto() {
//                            PictureSelector.create(mActivity)
//                                    .openCamera(PictureMimeType.ofImage())
//                                    .forResult(PictureConfig.CHOOSE_REQUEST);
                            SpUtil.putInt(DataCollectActivity.this,com.aids_malaria.app.Constant.CAMERATAG,1);
                            IDCardCamera.create(DataCollectActivity.this).openCamera(IDCardCamera.TYPE_IDCARD_BACK);
                        }

                        @Override
                        public void fromAlbum() {
                            PictureSelector.create(mActivity)
                                    .openGallery(PictureMimeType.ofImage())
                                    .compress(true)
                                    .forResult(PictureConfig.CHOOSE_REQUEST);
                        }
                    });
                }
                mHeadSelectDialog.show();
                break;
            case R.id.ll_base_data_switch:
                baseDataSwitch(); // 人员基本数据
                break;
            case R.id.ll_sick_heal_switch:
                sickHealSwitch(); //疟疾诊断/治疗
                break;
            case R.id.ll_test_result_switch:
                testResultSwitch(); // 检验结果
                break;
            case R.id.ll_clinical_symptoms_switch:
                clinicalSymptomsSwitch(); //临床症状
                break;
            case R.id.ll_sick_source_switch:
                sickSourceSwitch(); //感染来源调查
                break;
            case R.id.ll_sick_handle_switch:
                sickHandleSwitch(); //疫源处理
                break;
            case R.id.ll_medicine_heal_switch:
                if (TextUtils.isEmpty(et_heal_num.getText().toString().trim())
                        || TextUtils.isEmpty(et_diagnosis_man.getText().toString())
                        || TextUtils.isEmpty(et_diagnosis_time.getText().toString())){

                    ToastUtil.showToast(getString(R.string.edit_heal_first));
                    return;
                }
                medicineHealSwitch(); //药物治疗情况
                break;
            case R.id.ll_history_sick_switch:
                historySickSwitch();  //既往病史和治疗情况
                break;
            case R.id.ll_medical_secure_switch:
                medicalSecureSwitch(); //药物安全性
                break;
            case R.id.tv_gain_id:
                //获取身份id
                if (person_busy_type == 2){
                    ToastUtil.showToast(getString(R.string.id_update_forbid));
                    return;
                }
                if(StringUtil.isEmpty(current_county_code)){
                    ToastUtil.showToast(ResourceUtil.getString(R.string.please_select_count));
                    return;
                }
                if(StringUtil.isEmpty(et_birthday.getText().toString().trim())){
                    ToastUtil.showToast(ResourceUtil.getString(R.string.please_select_birthday));
                    return;
                }
                if(isOnLine()){
                    String padSeq = SpUtil.getString(mContext, Constant.PAD_SEQ, "");
                    getIdentityId(current_county_code, et_birthday.getText().toString().trim(),padSeq,"" + sex);
                }else{
                    String identityCode = StringUtil.getIdentity_id(current_county_code, et_birthday.getText().toString().trim(),
                            "" + sex);
                    et_id_num.setText(identityCode);
                }
                break;
            case R.id.iv_id_select:
                startActivityForResult(PersonSelectActivity.class,0,null);
                break;
            case R.id.iv_father_select:
            case R.id.et_father:
                // 获取父亲
                startActivityForResult(PersonSelectActivity.class,1,null);
                break;
            case R.id.iv_mother_select:
            case R.id.et_mother:
                // 获取母亲
                startActivityForResult(PersonSelectActivity.class,2,null);
                break;
            case R.id.tv_gain_xy:
                // 获取xy坐标
                getLocation();
                break;
            case R.id.tv_commit_base:
                // 提交人员基本数据
                commitBaseData();
                break;
            case R.id.tv_commit_history:
                // 提交既往病史...
                saveDiagnosisTreatment(2);
                break;
            case R.id.tv_medical_commit:
                //  提交药物安全性...
                saveDiagnosisTreatment(3);
                break;
            case R.id.tv_commit_heal_sit:
                //  提交诊断/治疗
                saveDiagnosisTreatment(1);
                break;
            case R.id.iv_birthday:
            case R.id.et_birthday:
                if (person_busy_type == 2){
                    return;
                }
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_birthday.setText(DateUtil.getYMDTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_investigate_time:
            case R.id.et_investigate_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_investigate_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                et_investigate_time.setText(DateUtil.getFullTimeFormatter().format(new Date()));
                break;
            case R.id.iv_diagnosis_time_select:
            case R.id.et_diagnosis_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_diagnosis_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_sick_upload_time:
            case R.id.et_sick_upload_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_sick_upload_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_mirror_time:
            case R.id.et_mirror_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_mirror_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_check_time:
            case R.id.et_check_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_check_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_check_time_source:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_check_time_source.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_eat_medicine_time:
            case R.id.et_eat_medicine_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_eat_medicine_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_hospital_time:
            case R.id.et_hospital_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_hospital_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_hospital_time2:
            case R.id.et_hospital_time2:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_hospital_time2.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_history_time_select:
            case R.id.et_history_investigate_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_history_investigate_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_medical_investigate_time:
            case R.id.et_medical_investigate_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_medical_investigate_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
            case R.id.iv_current_county_select:
            case R.id.et_county:
                bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY,Events.TYPE_BASE_DATA_CURRENT_ADDR);
                bundle.putBoolean("is_current",true);
                startActivity(CountySelectActivity.class,bundle);
                break;
            case R.id.iv_current_village_select:
            case R.id.et_village:
                bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY,Events.TYPE_BASE_DATA_CURRENT_ADDR);
                bundle.putString("county_code",current_county_code);
                bundle.putString("county_name",current_county_name);
                bundle.putBoolean("is_current",true);
                startActivity(VillageSelectActivity.class,bundle);
                break;
            case R.id.iv_birth_county_select:
            case R.id.et_county2:
                bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY,Events.TYPE_BASE_DATA_BIRTH_ADDR);
                startActivity(CountySelectActivity.class,bundle);
                break;
            case R.id.iv_birth_village_select:
            case R.id.et_village2:
                bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY,Events.TYPE_BASE_DATA_BIRTH_ADDR);
                bundle.putString("county_code",birth_county_code);
                bundle.putString("county_name",birth_county_name);
                startActivity(VillageSelectActivity.class,bundle);
                break;
            case R.id.iv_sick_place_county_select:
            case R.id.et_sick_address_county:
                bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY,Events.TYPE_SICK_PLACE);
                startActivity(CountySelectActivity.class,bundle);
                break;
            case R.id.iv_sick_place_village_select:
            case R.id.et_sick_address_village:
                bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY,Events.TYPE_SICK_PLACE);
                bundle.putString("county_code",sick_place_county_code);
                bundle.putString("county_name",sick_place_county_name);
                startActivity(VillageSelectActivity.class,bundle);
                break;
            case R.id.iv_sick_address_county2:
            case R.id.et_sick_address_county2:
                bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY,Events.TYPE_SICK_PLACE2);
                startActivity(CountySelectActivity.class,bundle);
                break;
            case R.id.iv_sick_address_village2:
            case R.id.et_sick_address_village2:
                bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY,Events.TYPE_SICK_PLACE2);
                bundle.putString("county_code",sick_place_county_code2);
                bundle.putString("county_name",sick_place_county_name2);
                startActivity(VillageSelectActivity.class,bundle);
                break;
            case R.id.iv_history_sick_time:
            case R.id.et_history_sick_time:
                DateUtil.newTimeSelect(this,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_history_sick_time.setText(DateUtil.getFullTimeFormatter().format(date));
                    }
                }).show();
                break;
        }
    }

    private LocationManager locationManager;
    private String provider = "";
    private LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            getLoadingDialog().dismiss();
            et_x.setText(String.format("%.6f", location.getLongitude()) + "");
            et_y.setText(String.format("%.6f", location.getLatitude()) + "");
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
            getLoadingDialog().dismiss();
        }

        @Override
        public void onProviderEnabled(String s) {
            getLoadingDialog().dismiss();
        }

        @Override
        public void onProviderDisabled(String s) {
            getLoadingDialog().dismiss();
        }
    };
    /**
     * 获取定位
     */
    @SuppressLint("MissingPermission")
    private void getLocation() {
        getLoadingDialog().show();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //获取所有可用的位置提供器
        List<String> providerList = locationManager.getProviders(true);
        if (providerList.contains(LocationManager.GPS_PROVIDER)) {
            provider = LocationManager.GPS_PROVIDER;
        } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
            provider = LocationManager.NETWORK_PROVIDER;
        } else {
            ToastUtil.showToast("No location provider to use");
        }
        Location location = locationManager.getLastKnownLocation(provider);
        if(location !=null){
            getLoadingDialog().dismiss();
            et_x.setText(String.format("%.6f", location.getLongitude()) + "");
            et_y.setText(String.format("%.6f", location.getLatitude()) + "");
        }
        locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(locationManager!=null){
            locationManager.removeUpdates(locationListener);
        }
    }

    /**
     * 村选择回调
     * @param villageSelectEvent
     */
    @Subscribe
    public void onCodeEvent(Events.VillageSelectEvent villageSelectEvent) {
        AreaV areaV = villageSelectEvent.getAreaV();
        if (villageSelectEvent.getType() == Events.TYPE_BASE_DATA_CURRENT_ADDR){
            current_village_code = areaV.getVillage_code();
            et_village.setText(areaV.getVillage_name());
        }else if (villageSelectEvent.getType() == Events.TYPE_BASE_DATA_BIRTH_ADDR){
            birth_village_code = areaV.getVillage_code();
            et_village2.setText(areaV.getVillage_name());
        }else if (villageSelectEvent.getType() == Events.TYPE_SICK_PLACE){
            sick_place_village_code = areaV.getVillage_code();
            et_sick_address_village.setText(areaV.getVillage_name());
        }else if (villageSelectEvent.getType() == Events.TYPE_SICK_PLACE2){
            sick_place_village_code2 = areaV.getVillage_code();
            et_sick_address_village2.setText(areaV.getVillage_name());
        }
    }

    /**
     * 县选择回调
     *
     * @param countySelectEvent
     */
    @Subscribe
    public void onCodeEvent(Events.CountySelectEvent countySelectEvent) {
        AreaC areaC = countySelectEvent.getAreaC();
        LogUtil.d("countySelectEvent:"+GsonUtil.beanToJson(countySelectEvent));
        LogUtil.d("areaC:"+GsonUtil.beanToJson(areaC));
        if (countySelectEvent.getType() == Events.TYPE_BASE_DATA_CURRENT_ADDR){
            current_county_code = areaC.getCounty_code();
            current_county_name = areaC.getCounty_name();
            et_county.setText(current_county_name);
            current_village_code = "";
            et_village.setText("");
        }else if (countySelectEvent.getType() == Events.TYPE_BASE_DATA_BIRTH_ADDR){
            birth_county_code = areaC.getCounty_code();
            birth_county_name = areaC.getCounty_name();
            et_county2.setText(birth_county_name);

            birth_village_code = "";
            et_village2.setText("");
        }else if (countySelectEvent.getType() == Events.TYPE_SICK_PLACE){
            sick_place_county_code = areaC.getCounty_code();
            sick_place_county_name = areaC.getCounty_name();
            et_sick_address_county.setText(sick_place_county_name);

            sick_place_village_code = "";
            et_sick_address_village.setText("");
        }else if (countySelectEvent.getType() == Events.TYPE_SICK_PLACE2){
            sick_place_county_code2 = areaC.getCounty_code();
            sick_place_county_name2 = areaC.getCounty_name();
            et_sick_address_county2.setText(sick_place_county_name2);

            sick_place_village_code2 = "";
            et_sick_address_village2.setText("");
        }
    }

    /**
     * 在线生成身份码
     * @param countyCode
     * @param birthday
     * @param padSeq
     * @param sex
     */
    private void getIdentityId(String countyCode, String birthday, String padSeq, String sex){
        RetrofitFactory.getInstance().createApi(RetrofitApi.class).getIdentityId(countyCode, birthday, padSeq, sex)
                .compose(RxScheduler.rxSchedulerTransform())
                .subscribe(new BaseObserver<String>() {

                    @Override
                    protected void onStart() {
                        getLoadingDialog().show();
                    }

                    @Override
                    protected void onSuccess(String data) throws Exception {
                        et_id_num.setText(data);
                    }

                    @Override
                    protected void onFinish() {
                        getLoadingDialog().dismiss();
                    }
                });
    }

    /**
     * 提交base数据
     */
    private void commitBaseData(){
        String id_num = et_id_num.getText().toString().trim();
        if (TextUtils.isEmpty(id_num)){
            ToastUtil.showToast(getString(R.string.hint_id_num));
            return;
        }
        if (TextUtils.isEmpty(et_name.getText().toString())){
            ToastUtil.showToast(getString(R.string.hint_name));
            return;
        }
        PersonData personData = new PersonData();
        personData.setIdentity_code(id_num); // 设置身份码
        personData.setGender(sex);
        personData.setPregnan_time(pregnant);

        personData.setName(et_name.getText().toString().trim());
        // 现居住
        personData.setResidential_adress_code("");
        if (!TextUtils.isEmpty(current_county_code)){
            personData.setResidential_adress_code(current_county_code);
        }
        if (!TextUtils.isEmpty(current_village_code)){
            personData.setResidential_adress_code(current_village_code);
        }
        // 出身地
        personData.setBirth_adress_code("");
        if (!TextUtils.isEmpty(birth_county_code)){
            personData.setBirth_adress_code(birth_county_code);
        }
        if (!TextUtils.isEmpty(birth_village_code)){
            personData.setBirth_adress_code(birth_village_code);
        }
        personData.setOriginal_id(et_id.getText().toString().trim()); // 设置原始id
        personData.setFather_identity_code(father_code);
        personData.setMother_identity_code(mother_code);
        String x = et_x.getText().toString().trim();
        personData.setX(TextUtils.isEmpty(x) ? 0 : Float.parseFloat(x));
        String y = et_y.getText().toString().trim();
        personData.setY(TextUtils.isEmpty(y) ? 0 : Float.parseFloat(y));
        personData.setPhone(et_g_phone.getText().toString().trim());
        personData.setTelephone(et_mobile_phone.getText().toString().trim());
        personData.setIs_die(die);
        personData.setInvestigator(et_investigate_man.getText().toString().trim());
        personData.setInvestigate_date(et_investigate_time.getText().toString().trim());
        personData.setBirthday(et_birthday.getText().toString().trim());
        personData.setResidential_adress_detail(et_address_detail.getText().toString().trim());
        personData.setBirth_adress_detail(et_birth_place_detail.getText().toString().trim());
        personData.setPhoto(photo); // 照片
        personData.setBusy_type(person_busy_type);

        personData.setResidential_county_name(et_county.getText().toString().trim());
        personData.setResidential_village_name(et_village.getText().toString().trim());
        personData.setBirth_county_name(et_county2.getText().toString().trim());
        personData.setBirth_village_name(et_village2.getText().toString());

        if (isOnLine()){
            // 在线状态
            String json = GsonUtil.beanToJson(personData);
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
            RetrofitFactory.getInstance().createApi(RetrofitApi.class).savePersonData(person_busy_type, requestBody)
                    .compose(RxScheduler.rxSchedulerTransform())
                    .subscribe(new BaseObserver<Object>() {

                        @Override
                        protected void onStart() {
                            getLoadingDialog().show();
                        }

                        @Override
                        protected void onSuccess(Object data) throws Exception {
                            if(person_busy_type == 1){
                                //新增
                                String sPad_id = SpUtil.getString(App.getAppContext(), Constant.PAD_SEQ, "");
                                sPad_id = sPad_id.substring(sPad_id.length()-2,sPad_id.length());
                                List<PadMaxIdentitySeq> padMaxIdentitySeqList = DBManager.get().padMaxIdentitySeqDao().getByCodeAndSex(
                                        current_county_code + et_birthday.getText().toString().trim().replaceAll("-", "") + sPad_id, "" + sex);
                                if(padMaxIdentitySeqList.size()>0){
                                    PadMaxIdentitySeq padMaxIdentitySeq = padMaxIdentitySeqList.get(0);
                                    if("1".equals("" + sex)){
                                        padMaxIdentitySeq.setMax_identityseq(String.valueOf(Integer.valueOf(padMaxIdentitySeq.getMax_identityseq()+2)));
                                    } else{
                                        padMaxIdentitySeq.setMax_identityseq(String.valueOf(Integer.valueOf(padMaxIdentitySeq.getMax_identityseq()+2)));
                                    }
                                    LogUtil.d("padMaxIdentitySeq:"+GsonUtil.beanToJson(padMaxIdentitySeq));
                                    DBManager.get().padMaxIdentitySeqDao().update(padMaxIdentitySeq);
                                }else{
                                    PadMaxIdentitySeq padMaxIdentitySeq = new PadMaxIdentitySeq();
                                    padMaxIdentitySeq.setIdentity_subfield(current_county_code + et_birthday.getText().toString().trim().replaceAll("-", "") + sPad_id);
                                    padMaxIdentitySeq.setSex("" + sex);
                                    if("1".equals("" + sex)){
                                        padMaxIdentitySeq.setMax_identityseq("001");
                                    } else{
                                        padMaxIdentitySeq.setMax_identityseq("002");
                                    }
                                    LogUtil.d("padMaxIdentitySeq:"+GsonUtil.beanToJson(padMaxIdentitySeq));
                                    DBManager.get().padMaxIdentitySeqDao().insert(padMaxIdentitySeq);
                                }
                            }
                            ToastUtil.showToast(getString(R.string.save_success));
                            person_busy_type = 2;
                        }

                        @Override
                        protected void onFinish() {
                            getLoadingDialog().dismiss();
                        }
                    });
        }else {
            // 离线状态，保存到本地数据库
            if (person_busy_type == 1){
                personData.setCreateOrUpdate(true);
                Long insert = DBManager.get().personDataDao().insert(personData);
                if (insert > 0){
                    if(person_busy_type == 1){
                        //新增
                        String sPad_id = SpUtil.getString(App.getAppContext(), Constant.PAD_SEQ, "");
                        sPad_id = sPad_id.substring(sPad_id.length()-2,sPad_id.length());
                        List<PadMaxIdentitySeq> padMaxIdentitySeqList = DBManager.get().padMaxIdentitySeqDao().getByCodeAndSex(
                                current_county_code + et_birthday.getText().toString().trim().replaceAll("-", "") + sPad_id, "" + sex);
                        if(padMaxIdentitySeqList.size()>0){
                            PadMaxIdentitySeq padMaxIdentitySeq = padMaxIdentitySeqList.get(0);
                            if("1".equals("" + sex)){
                                padMaxIdentitySeq.setMax_identityseq(String.valueOf(Integer.valueOf(padMaxIdentitySeq.getMax_identityseq()+2)));
                            } else{
                                padMaxIdentitySeq.setMax_identityseq(String.valueOf(Integer.valueOf(padMaxIdentitySeq.getMax_identityseq()+2)));
                            }
                            LogUtil.d("padMaxIdentitySeq:"+GsonUtil.beanToJson(padMaxIdentitySeq));
                            DBManager.get().padMaxIdentitySeqDao().update(padMaxIdentitySeq);
                        }else{
                            PadMaxIdentitySeq padMaxIdentitySeq = new PadMaxIdentitySeq();
                            padMaxIdentitySeq.setIdentity_subfield(current_county_code + et_birthday.getText().toString().trim().replaceAll("-", "") + sPad_id);
                            padMaxIdentitySeq.setSex("" + sex);
                            if("1".equals("" + sex)){
                                padMaxIdentitySeq.setMax_identityseq("001");
                            } else{
                                padMaxIdentitySeq.setMax_identityseq("002");
                            }
                            LogUtil.d("padMaxIdentitySeq:"+GsonUtil.beanToJson(padMaxIdentitySeq));
                            DBManager.get().padMaxIdentitySeqDao().insert(padMaxIdentitySeq);
                        }
                    }
                    person_busy_type = 2;
                    ToastUtil.showToast(getString(R.string.save_success));
                }
            }else if (person_busy_type == 2){
//                int insert = LiteOrmUtil.update(personData);
                personData.setCreateOrUpdate(true);
                int insert = DBManager.get().personDataDao().update(personData);
                if (insert > 0){
                    ToastUtil.showToast(getString(R.string.save_success));
                }
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 3){
            PersonData personData = (PersonData) data.getSerializableExtra("personData");
            et_father.setText(personData.getName());
            father_code = personData.getIdentity_code();
        }else if (requestCode == 2 && resultCode == 3){
            PersonData personData = (PersonData) data.getSerializableExtra("personData");
            et_mother.setText(personData.getName());
            mother_code = personData.getIdentity_code();
        }else if (requestCode == 0 && resultCode == 3){
            PersonData personData = (PersonData) data.getSerializableExtra("personData");
            et_id.setText(personData.getOriginal_id());
            // et_id_num.setText(personData.getIdentity_code());
            // 选取后获取改人的所有信息
            //清除序号
            et_heal_num.setText("");
            et_heal_sit_num.setText("");
            et_handle_num.setText("");
            et_medical_num.setText("");
            et_history_num.setText("");
            GetDiagnosisTreatmentInfo(personData.getIdentity_code());
            ll_medicine_heal.setVisibility(View.GONE); // 收缩药物治疗情况
            iv_medicine_heal.setImageResource(R.mipmap.add);
        }
        if (resultCode == IDCardCamera.RESULT_CODE) {
            final String path1 = IDCardCamera.getImagePath(data);
            if (!TextUtils.isEmpty(path1)) {
                Bitmap bitmap1 = BitmapFactory.decodeFile(path1);
                photo = BitmapUtil.bitmapToBase64(bitmap1);
                iv_photo.setImageBitmap(bitmap1);
                camera_bg.setVisibility(View.GONE);
                iv_photo.setVisibility(View.VISIBLE);
            }else{
                IDCardCamera.create(DataCollectActivity.this).openCamera(IDCardCamera.TYPE_IDCARD_BACK);
            }
            return;
        }
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    camera_bg.setVisibility(View.GONE);
                    iv_photo.setVisibility(View.VISIBLE);
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回五种path
                    // 1.media.getPath(); 原图path，但在Android Q版本上返回的是content:// Uri类型
                    // 2.media.getCutPath();裁剪后path，需判断media.isCut();切勿直接使用
                    // 3.media.getCompressPath();压缩后path，需判断media.isCompressed();切勿直接使用
                    // 4.media.getOriginalPath()); media.isOriginal());为true时此字段才有值
                    // 5.media.getAndroidQToPath();Android Q版本特有返回的字段，但如果开启了压缩或裁剪还是取裁剪或压缩路径；注意：.isAndroidQTransform(false);此字段将返回空
                    // 如果同时开启裁剪和压缩，则取压缩路径为准因为是先裁剪后压缩
                    for (LocalMedia media : selectList) {
                        String compressPath = media.getCompressPath(); // 压缩
                        String path = media.getPath(); // 原图
                        String cutPath = media.getCutPath(); // 裁剪
                        LogUtil.d("压缩::" + compressPath);
                        LogUtil.d( "原图::" + path);
                        LogUtil.d("裁剪::" + cutPath);

                        FileInputStream fis = null;
                        try {
                            fis = new FileInputStream(!TextUtils.isEmpty(compressPath) ? compressPath : path);
                            Bitmap bitmap  = BitmapFactory.decodeStream(fis);
                            Bitmap compressImage = BitmapUtil.compressImage(bitmap);
                            photo = BitmapUtil.bitmapToBase64(compressImage);
                            iv_photo.setImageBitmap(compressImage);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
        }
    }


    /**
     * 展示person的数据
     * @param person
     */
    private void showPersonData(List<PersonData> person){
        if (person != null && person.size() > 0){
            PersonData personData = person.get(0);
            person_busy_type = 2; // 修改编辑
            // 禁止某些信息修改
            et_id_num.setEnabled(false);
            rb_male.setEnabled(false);
            rb_female.setEnabled(false);

            et_id.setText(personData.getOriginal_id());
            et_id_num.setText(personData.getIdentity_code());
            et_name.setText(personData.getName());
            et_birthday.setText(personData.getBirthday());
            sex = personData.getGender();
            photo = personData.getPhoto();
            mother_code = personData.getMother_identity_code();
            father_code = personData.getFather_identity_code();
            et_father.setText(father_code);
            et_mother.setText(mother_code);

            et_county.setText(personData.getResidential_county_name());
            et_village.setText(personData.getResidential_village_name());
            et_county2.setText(personData.getBirth_county_name());
            et_village2.setText(personData.getBirth_village_name());

            if (!TextUtils.isEmpty(photo)){
                try{
                    Bitmap bitmap = BitmapUtil.base64ToBitmap(photo);
                    iv_photo.setImageBitmap(bitmap);
                    camera_bg.setVisibility(View.GONE);
                    iv_photo.setVisibility(View.VISIBLE);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                // 如果没头像，清空头像，有可能是上一个人的
                iv_photo.setImageBitmap(null);
                camera_bg.setVisibility(View.VISIBLE);
                iv_photo.setVisibility(View.GONE);
            }
            birth_village_code = personData.getBirth_adress_code();
            current_village_code = personData.getResidential_adress_code();
            // et_village2.setText(personData.getVillage_name());
            switch (personData.getGender()){
                case 1:
                    rb_male.setChecked(true);
                    break;
                case 2:
                    rb_female.setChecked(true);
                    break;
            }
            et_address_detail.setText(personData.getResidential_adress_detail());
            et_birth_place_detail.setText(personData.getBirth_adress_detail());
            et_father.setText(personData.getFather_identity_code());
            et_mother.setText(personData.getMother_identity_code());
            et_x.setText(personData.getX() + "");
            et_y.setText(personData.getY() + "");
            et_mobile_phone.setText(personData.getTelephone());
            et_g_phone.setText(personData.getPhone());
            et_investigate_man.setText(personData.getInvestigator());
            et_investigate_time.setText(personData.getInvestigate_date());
            pregnant = personData.getPregnan_time();
            switch (personData.getPregnan_time()){
                case 0:
                    rb_not_pregnant.setChecked(true);
                    break;
                case 1:
                    rb_less_3.setChecked(true);
                    break;
                case 2:
                    rb_more_3.setChecked(true);
                    break;
            }
            die = personData.getIs_die();
            switch (personData.getIs_die()){
                case 1:
                    rb_yes_die.setChecked(true);
                    break;
                case 0:
                    rb_not_die.setChecked(true);
                    break;
            }
        }else {
            person_busy_type = 1;
        }
    }

    /**
     * 展示药物安全性数据
     */
    private void showDrugallergyData(int recordSeq){
        if (drugallergy_infos != null && drugallergy_infos.size() > 0){
            DrugallergyInfoPack drugallergyInfo = null;
            if (recordSeq == 0){
                drugallergyInfo = drugallergy_infos.get(0);
            }else {
                for (int i = 0; i < drugallergy_infos.size(); i++) {
                    DrugallergyInfoPack drugallergyInfoPack = drugallergy_infos.get(i);
                    if (drugallergyInfoPack.getRecord_seq() == recordSeq){
                        drugallergyInfo = drugallergyInfoPack;
                        break;
                    }
                }
            }
            if (drugallergyInfo == null){
                clearDrugallergyInfo();
                return;
            }
            drugallergy_busy_type = 2; // 修改编辑
            et_medical_num.setText(drugallergyInfo.getRecord_seq() + "");
            et_allergy_medicine.setText(drugallergyInfo.getAllergy_drug());
            et_allergy_history.setText(drugallergyInfo.getAllergy_info());
            et_medical_investigate.setText(drugallergyInfo.getInvestor());
            et_medical_investigate_time.setText(drugallergyInfo.getInvest_time());
            AdverseReaction adverseReaction = drugallergyInfo.getAdverse_reactions();
//                                AdverseReaction adverseReaction = GsonUtil.gsonToBean(adverse_reactions, AdverseReaction.class);
            cb_eat_feel_down.setChecked(adverseReaction.getAnorexia() == 1 ? true : false);
            cb_nausea.setChecked(adverseReaction.getNausea() == 1 ? true : false);
            cb_vomit.setChecked(adverseReaction.getVomiting() == 1 ? true : false);
            cb_diarrhea.setChecked(adverseReaction.getDiarrhea() == 1 ? true : false);
            cb_heart_ele_abnormal.setChecked(adverseReaction.getAbnormalECG() == 1 ? true : false);
            cb_hemolysis.setChecked(adverseReaction.getHemolysis() == 1 ? true : false);
            String other = adverseReaction.getOther();
            cb_medical_other.setChecked(TextUtils.isEmpty(other) ? false : true);
            et_medical_other.setText(other);
        }else {
            clearDrugallergyInfo();
        }
    }

    private void clearDrugallergyInfo(){
        drugallergy_busy_type = 1; // 新增
        // 清空数据
//        et_medical_num.setText("");
        et_allergy_medicine.setText("");
        et_allergy_history.setText("");
        et_medical_investigate.setText("");
        et_medical_investigate_time.setText("");
        cb_eat_feel_down.setChecked(false);
        cb_nausea.setChecked(false);
        cb_vomit.setChecked(false);
        cb_diarrhea.setChecked(false);
        cb_heart_ele_abnormal.setChecked(false);
        cb_hemolysis.setChecked(false);
        cb_medical_other.setChecked(false);
        et_medical_other.setText("");
    }


    /**
     * 展示既往病史数据
     */
    private void showPremedicalTreatmentInfo(int recordSeq){
        if (premedical_treatments != null && premedical_treatments.size() > 0){
            PremedicalTreatmentPack premedicalTreatment = null;
            if (recordSeq == 0){
                premedicalTreatment = premedical_treatments.get(0);
            }else {
                for (int i = 0; i < premedical_treatments.size(); i++) {
                    PremedicalTreatmentPack premedicalTreatmentPack = premedical_treatments.get(i);
                    if (premedicalTreatmentPack.getRecord_seq() == recordSeq){
                        premedicalTreatment = premedicalTreatmentPack;
                        break;
                    }
                }
            }
            if (premedicalTreatment == null){
                clearPremedicalTreatment();
                return;
            }
            premedical_busy_type = 2;// 修改编辑
            et_history_num.setText(premedicalTreatment.getRecord_seq() + "");
            et_history_sick_time.setText(premedicalTreatment.getMalaria_time());
            et_medical_days.setText(premedicalTreatment.getTaken_days());
            et_history_investigate.setText(premedicalTreatment.getInvestor());
            et_history_investigate_time.setText(premedicalTreatment.getInvest_time());
            String is_root_therapy = premedicalTreatment.getIs_root_therapy();
            try{
                is_xg = Integer.parseInt(is_root_therapy);
            }catch (Exception e){
                e.printStackTrace();
            }
            if (TextUtils.equals(is_root_therapy,"1")){
                rb_xg_yes.setChecked(true);
            }else {
                rb_xg_not.setChecked(true);
            }
            TokenDrug tokenDrug = premedicalTreatment.getTaken_drags();
//                                TokenDrug tokenDrug = GsonUtil.gsonToBean(taken_drags, TokenDrug.class);
            cb_medicine1.setChecked(tokenDrug.getArtesunatepiperaquinetablets() == 1 ? true : false);
            cb_medicine2.setChecked(tokenDrug.getArtesunatebenflumetoltablets() == 1 ? true : false);
            cb_medicine3.setChecked(tokenDrug.getArtesunateamodiquinetablets() == 1 ? true : false);
            cb_medicine4.setChecked(tokenDrug.getDihydroartemisininphosphatepiperaquinetablets() == 1 ? true : false);
            cb_medicine5.setChecked(tokenDrug.getArtesunatesulfadoxinepyrimethaminetablets() == 1 ? true : false);
            cb_medicine6.setChecked(tokenDrug.getArtesunateinjection() == 1 ? true : false);
            cb_medicine7.setChecked(tokenDrug.getArtemetherinjection() == 1 ? true : false);
            cb_medicine8.setChecked(tokenDrug.getPrimaquine() == 1 ? true : false);
            cb_medicine9.setChecked(tokenDrug.getSulfadoxinepyrimethaminesp() == 1 ? true : false);
            cb_medicine10.setChecked(tokenDrug.getChloroquine() == 1 ? true : false);
            String other = tokenDrug.getOther();
            cb_medicine_other.setChecked(TextUtils.isEmpty(other) ? false : true);
            et_medicine_other.setText(other);

        }else {
            clearPremedicalTreatment();
        }
    }

    private void clearPremedicalTreatment(){
        premedical_busy_type = 1; // 新增
        // 清空数据
//        et_history_num.setText("");
        et_history_sick_time.setText("");
        et_medical_days.setText("");
        et_history_investigate.setText("");
        et_history_investigate_time.setText("");
        is_xg = 1;
        rb_xg_yes.setChecked(true);
        cb_medicine1.setChecked(false);
        cb_medicine2.setChecked(false);
        cb_medicine3.setChecked(false);
        cb_medicine4.setChecked(false);
        cb_medicine5.setChecked(false);
        cb_medicine6.setChecked(false);
        cb_medicine7.setChecked(false);
        cb_medicine8.setChecked(false);
        cb_medicine9.setChecked(false);
        cb_medicine10.setChecked(false);
        cb_medicine_other.setChecked(false);
        et_medicine_other.setText("");
    }

    /**
     * 设置诊疗数据
     */
    private void showDiagnosisInfo(int recordSeq){
        if (diagnosis_infos != null && diagnosis_infos.size() > 0){
            DiagnosisInfoPack diagnosisInfo = null;
            if (recordSeq == 0){
                diagnosisInfo = diagnosis_infos.get(0);
            }else {
                for (int i = 0; i < diagnosis_infos.size(); i++) {
                    DiagnosisInfoPack diagnosisInfoPack = diagnosis_infos.get(i);
                    if (diagnosisInfoPack.getRecord_no() == recordSeq){
                        diagnosisInfo = diagnosisInfoPack;
                        break;
                    }
                }
            }
            if (diagnosisInfo == null){
                clearDiagnosisInfo();
                return;
            }
            diagnosis_busy_type = 2 ;// 有的话就编辑修改
            sick_place_village_code = diagnosisInfo.getOnset_place();
            sick_place_village_code2 = diagnosisInfo.getEa_place();
            et_heal_num.setText(diagnosisInfo.getRecord_no() + "");
            et_diagnosis_man.setText(diagnosisInfo.getDiagnostics());
            et_diagnosis_time.setText(diagnosisInfo.getDiagnostic_time());
            int diagnosis_result = diagnosisInfo.getDiagnosis_result();
            heal_result = diagnosis_result;
            switch (diagnosis_result){
                case 0:
                    rb_noomal.setChecked(true);
                    break;
                case 1:
                    rb_uncertain.setChecked(true);
                    break;
                case 2:
                    rb_certain.setChecked(true);
                    break;
            }
            et_sick_upload_time.setText(diagnosisInfo.getReport_time());
            et_report_unit.setText(diagnosisInfo.getReport_unit());
            et_reporter.setText(diagnosisInfo.getReportor());
            // 症状
            Symptom symptom = diagnosisInfo.getClinical_symptoms();
            // Symptom symptom = GsonUtil.gsonToBean(clinical_symptoms, Symptom.class);
            cb_temperature_normal.setChecked(symptom.getNormothermia() == 1 ? true : false);
            cb_last_hot.setChecked(symptom.getContinuousferer() == 1 ? true : false);
            cb_other_day_hot.setChecked(symptom.getQodferer() == 1 ? true : false);
            cb_hot_suddenly.setChecked(symptom.getIrregularheatinggap() == 1 ? true : false);
            cb_cold.setChecked(symptom.getFeeldizzy() == 1 ? true : false);
            cb_sweat.setChecked(symptom.getSweat() == 1 ? true : false);
            cb_headache.setChecked(symptom.getHeadache() == 1 ? true : false);
            cb_diarrhea2.setChecked(symptom.getDiarrhea() == 1 ? true : false);
            cb_muscle_ache.setChecked(symptom.getMusclesoreness() == 1 ? true : false);
            // 兼症
            ConSymptom conSymptom = diagnosisInfo.getCon_symptoms();
//                                ConSymptom conSymptom = GsonUtil.gsonToBean(con_symptoms, ConSymptom.class);
            cb_shiver.setChecked(conSymptom.getShiver() == 1 ? true : false);
            cb_twitch.setChecked(conSymptom.getTic() == 1 ? true : false);
            cb_shock.setChecked(conSymptom.getShock() == 1 ? true : false);
            cb_oliguria.setChecked(conSymptom.getOliguria() == 1 ? true : false);
            cb_hematuria.setChecked(conSymptom.getHematuria() == 1 ? true : false);
            String other = conSymptom.getOther();
            cb_other_symptoms.setChecked(TextUtils.isEmpty(other) ? false : true);
            et_other_symptoms.setText(other);
            et_sick_address_county.setText(diagnosisInfo.getOnset_county_name());
            et_sick_address_village.setText(diagnosisInfo.getOnset_village_name());
            et_detail_address.setText(diagnosisInfo.getOnset_place_detail());
            // 镜检结果
            MicroResults microResults = diagnosisInfo.getMicro_results();
//                                MicroResults microResults = GsonUtil.gsonToBean(micro_results, MicroResults.class);
            cb_negative.setChecked(microResults.getNegative() == 1 ? true : false);
            cb_tertian_malaria.setChecked(microResults.getTertian_malaria() == 1 ? true : false);
            cb_malignant_malaria.setChecked(microResults.getFalciparum_malaria() == 1 ? true : false);
            if (microResults.getFalciparum_malaria() == 1){
                ll_ex1.setVisibility(View.VISIBLE);
                ll_ex2.setVisibility(View.VISIBLE);
            }else {
                ll_ex1.setVisibility(View.GONE);
                ll_ex2.setVisibility(View.GONE);
            }
            cb_quartan_malaria.setChecked(microResults.getQuartan_malaria() == 1 ? true : false);
            cb_ovale_malaria.setChecked(microResults.getOvale_malaria() == 1 ? true : false);
            cb_mixed_infection.setChecked(microResults.getMixed_infection() == 1 ? true : false);
            et_protozoan_density.setText(diagnosisInfo.getProtozoan_density() + "");// 原虫密度
            et_gametophyte_carrying_rate.setText(diagnosisInfo.getGametophyte_carry_rate() + "");
            et_ratio_of_male_to_female_gametophyte.setText(diagnosisInfo.getMtf_gametophyte_rate() + "");
            et_mirror_man.setText(diagnosisInfo.getMicro_examiner());
            et_mirror_time.setText(diagnosisInfo.getMicro_time());
            // rdt镜检结果
            MicroResults rdtResults = diagnosisInfo.getRdt_result();
//                                MicroResults rdtResults = GsonUtil.gsonToBean(rdt_results, MicroResults.class);
            cb_negative2.setChecked(rdtResults.getNegative() == 1 ? true : false);
            cb_tertian_malaria2.setChecked(rdtResults.getTertian_malaria() == 1 ? true : false);
            cb_malignant_malaria2.setChecked(rdtResults.getFalciparum_malaria() == 1 ? true : false);
            cb_quartan_malaria2.setChecked(rdtResults.getQuartan_malaria() == 1 ? true : false);
            cb_ovale_malaria2.setChecked(rdtResults.getOvale_malaria() == 1 ? true : false);
            cb_mixed_infection2.setChecked(rdtResults.getMixed_infection() == 1 ? true : false);
            et_checker.setText(diagnosisInfo.getRdt_checker());
            et_check_time.setText(diagnosisInfo.getRdt_check_time());
            // 2周内是否有其他流行区居留史
            int is_roea = diagnosisInfo.getIs_roea();
            is_role = is_roea;
            switch (is_roea){
                case 1:
                    rb_2week_yes.setChecked(true);
                    break;
                case 0:
                    rb_2week_not.setChecked(true);
                    break;
            }
            et_sick_address_county2.setText(diagnosisInfo.getEa_county_name());
            et_sick_address_village2.setText(diagnosisInfo.getEa_village_name());
            et_detail_address2.setText(diagnosisInfo.getOea_place());
            //发病前2周内是否有输血史
            is_transfusion_twoweek = diagnosisInfo.getIs_transfusion_twoweek();
            switch (is_transfusion_twoweek){
                case 1:
                    rb_2week_blood_yes.setChecked(true);
                    break;
                case 0:
                    rb_2week_blood_not.setChecked(true);
                    break;
            }
            // 近1月内家庭成员或来访亲友是否有人发热
            is_visitor_fever = diagnosisInfo.getIs_visitor_fever();
            switch (is_visitor_fever){
                case 1:
                    rb_1week_family_hot_yes.setChecked(true);
                    break;
                case 0:
                    rb_1week_family_hot_not.setChecked(true);
                    break;
            }
            et_checker_source.setText(diagnosisInfo.getInvestor());
            et_check_time_source.setText(diagnosisInfo.getInvest_time());

            infection_source_type = diagnosisInfo.getInfection_source_type();
            switch (infection_source_type){
                case 1:
                    rb_local_village_sick.setChecked(true);
                    break;
                case 2:
                    rb_outside_village_sick.setChecked(true);
                    break;
                case 4:
                    rb_outside_sick.setChecked(true);
                    break;
            }

        }else {
            clearDiagnosisInfo();
        }
    }

    private void clearDiagnosisInfo(){
        diagnosis_busy_type = 1; // 空的话变为新增
        // 清空界面
        sick_place_village_code = "";
        sick_place_village_code2 = "";
//        et_heal_num.setText("");
        et_diagnosis_man.setText("");
        et_diagnosis_time.setText("");
        rb_noomal.setChecked(true);
        et_sick_upload_time.setText("");
        et_report_unit.setText("");
        et_reporter.setText("");
        heal_result = 0;
        et_sick_address_county.setText("");
        et_sick_address_village.setText("");
        et_detail_address.setText("");
        et_sick_address_county2.setText("");
        et_sick_address_village2.setText("");
        et_detail_address2.setText("");
        // 症状
        cb_temperature_normal.setChecked(false);
        cb_last_hot.setChecked(false);
        cb_other_day_hot.setChecked(false);
        cb_hot_suddenly.setChecked(false);
        cb_cold.setChecked(false);
        cb_sweat.setChecked(false);
        cb_headache.setChecked(false);
        cb_diarrhea2.setChecked(false);
        cb_muscle_ache.setChecked(false);
        // 兼症
        cb_shiver.setChecked(false);
        cb_twitch.setChecked(false);
        cb_shock.setChecked(false);
        cb_oliguria.setChecked(false);
        cb_hematuria.setChecked(false);
        cb_other_symptoms.setChecked(false);
        et_other_symptoms.setText("");
        et_sick_address_county.setText("");
        // 镜检结果
        cb_negative.setChecked(false);
        cb_tertian_malaria.setChecked(false);
        cb_malignant_malaria.setChecked(false);
        ll_ex1.setVisibility(View.GONE);
        ll_ex2.setVisibility(View.GONE);
        cb_quartan_malaria.setChecked(false);
        cb_ovale_malaria.setChecked(false);
        cb_mixed_infection.setChecked(false);
        et_protozoan_density.setText("");// 原虫密度
        et_gametophyte_carrying_rate.setText("");
        et_ratio_of_male_to_female_gametophyte.setText("");
        et_mirror_man.setText("");
        et_mirror_time.setText("");
        // rdt镜检结果
        cb_negative2.setChecked(false);
        cb_tertian_malaria2.setChecked(false);
        cb_malignant_malaria2.setChecked(false);
        cb_quartan_malaria2.setChecked(false);
        cb_ovale_malaria2.setChecked(false);
        cb_mixed_infection2.setChecked(false);
        et_checker.setText("");
        et_check_time.setText("");
        // 2周内是否有其他流行区居留史
        rb_2week_not.setChecked(true);
        et_sick_address_county2.setText("");
        //发病前2周内是否有输血史
        rb_2week_blood_yes.setChecked(true);
        // 近1月内家庭成员或来访亲友是否有人发热
        rb_1week_family_hot_yes.setChecked(true);
        et_checker_source.setText("");
        et_check_time_source.setText("");
        rb_local_village_sick.setChecked(true);
        is_role = 0; // 发病前2周内是否有其他流行区居留史
        is_transfusion_twoweek = 1; // 发病前2周内是否有输血史
        is_visitor_fever = 1; // 近1月内家庭成员或来访亲友是否有人发热
        infection_source_type = 1; // 感染来源分类
    }

    /**
     * 疫源处理
     */
    private void showEaProcessInfo(int recordReq){
        if (ea_processs != null && ea_processs.size() > 0){
            String heal_num = et_heal_num.getText().toString();
            if (TextUtils.isEmpty(heal_num)){
                clearEaData();
                return;
            }
            EaProcessPack eaProcess = null;
            if (recordReq == 0){
                if(ea_processs.get(0).getRecord_no() == Integer.parseInt(heal_num)){
                    eaProcess = ea_processs.get(0);
                }
            } else {
                for (int i = 0; i < ea_processs.size(); i++) {
                    EaProcessPack eaProcessPack = ea_processs.get(i);
                    if (eaProcessPack.getRecord_seq() == recordReq && eaProcessPack.getRecord_no() == Integer.parseInt(heal_num)){
                        eaProcess = eaProcessPack;
                        break;
                    }
                }
            }
            if (eaProcess == null){
                clearEaData();
                return;
            }
            ea_busy_type = 2; // 修改编辑
            EaProcessPack.OutdoorSprayAgent outdoor_spray_agent = eaProcess.getOutdoor_spray_agent();
            if (outdoor_spray_agent.getPirimiphos_methyl() == 1){
                rb_jjmdl.setChecked(true);
            }
            if (!TextUtils.isEmpty(outdoor_spray_agent.getOther())){
                rb_other_ps.setChecked(true);
                et_other_ps.setText(outdoor_spray_agent.getOther());
            }
            et_handle_num.setText(eaProcess.getRecord_seq() + "");
            et_ps_time.setText(eaProcess.getSpray_time());
            et_inside_ps_thing.setText(eaProcess.getIndoor_spray_agent());
            et_other_ps.setText(eaProcess.getOutdoor_spray_agent().getOther());
            out_range = eaProcess.getOut_range();
            switch (out_range){
                case 1:
                    rb_metre_50.setChecked(true);
                    break;
                case 2:
                    rb_over_50m.setChecked(true);
                    break;
                case 3:
                    rb_over_100m.setChecked(true);
                    break;
            }
            bednets_mu_rate = eaProcess.getBednets_mu_rate();
            switch (bednets_mu_rate){
                case 1:
                    rb_day_10.setChecked(true);
                    break;
                case 2:
                    rb_over_10day.setChecked(true);
                    break;
                case 3:
                    rb_over_20day.setChecked(true);
                    break;
            }
            et_handle_checker.setText(eaProcess.getInvestor());

        }else {
            clearEaData();
        }
    }

    private void clearEaData(){
        ea_busy_type = 1; // 新增
        // 清空数据
//        et_handle_num.setText("");
        rb_jjmdl.setChecked(false);
        rb_other_ps.setChecked(false);
        et_other_ps.setText("");
//        et_handle_num.setText("");
        et_ps_time.setText("");
        et_inside_ps_thing.setText("");
        et_other_ps.setText("");
        rb_metre_50.setChecked(true);
        rb_day_10.setChecked(true);
        et_handle_checker.setText("");
        out_range = 1; // 室外喷洒范围
        bednets_mu_rate = 1; // 每月使用蚊帐率
    }

    private void showGetMedicalTreatmentInfo(int recordSeq,boolean isFirstLoad){
        if (meical_treatments != null && meical_treatments.size() > 0){
            String heal_num = et_heal_num.getText().toString();
            if (TextUtils.isEmpty(heal_num)){
                clearGetMedicalData();
                return;
            }
            GetMedicalTreatmentPack getMedicalTreatment = null;
            if (recordSeq == 0){
                if(meical_treatments.get(0).getRecord_no() == Integer.parseInt(heal_num)){
                    getMedicalTreatment = meical_treatments.get(0);
                }
            }else {
                for (int i = 0; i < meical_treatments.size(); i++) {
                    GetMedicalTreatmentPack getMedicalTreatmentPack = meical_treatments.get(i);
                    if (getMedicalTreatmentPack.getRecord_seq() == recordSeq
                            && getMedicalTreatmentPack.getRecord_no() == Integer.parseInt(heal_num)){
                        getMedicalTreatment = getMedicalTreatmentPack;
                        break;
                    }
                }
            }
            if(getMedicalTreatment == null){
//                et_heal_sit_num.setText("");
                medical_busy_type = 1;
                clearGetMedicalData();
                return;
            }
            medical_busy_type = 2; // 修改编辑
            et_heal_sit_num.setText(getMedicalTreatment.getRecord_seq() + "");
            et_eat_medicine_time.setText(getMedicalTreatment.getMedicine_taken_time());
            TokenDrug tokenDrug = getMedicalTreatment.getTaken_drags();
//                                TokenDrug tokenDrug = GsonUtil.gsonToBean(taken_drags, TokenDrug.class);
            cb2_medicine1.setChecked(tokenDrug.getArtesunatepiperaquinetablets() == 1 ? true : false);
            cb2_medicine2.setChecked(tokenDrug.getArtesunatebenflumetoltablets() == 1 ? true : false);
            cb2_medicine3.setChecked(tokenDrug.getArtesunateamodiquinetablets() == 1 ? true : false);
            cb2_medicine4.setChecked(tokenDrug.getDihydroartemisininphosphatepiperaquinetablets() == 1 ? true : false);
            cb2_medicine5.setChecked(tokenDrug.getArtesunatesulfadoxinepyrimethaminetablets() == 1 ? true : false);
            cb2_medicine6.setChecked(tokenDrug.getArtesunateinjection() == 1 ? true : false);
            cb2_medicine7.setChecked(tokenDrug.getArtemetherinjection() == 1 ? true : false);
            cb2_medicine8.setChecked(tokenDrug.getPrimaquine() == 1 ? true : false);
            cb2_medicine9.setChecked(tokenDrug.getSulfadoxinepyrimethaminesp() == 1 ? true : false);
            cb2_medicine10.setChecked(tokenDrug.getChloroquine() == 1 ? true : false);
            String other = tokenDrug.getOther();
            cb2_medicine_other.setChecked(TextUtils.isEmpty(other) ? false : true);
            et2_medicine_other.setText(other);
            is_hospital = getMedicalTreatment.getIs_hospital();
            switch (is_hospital){
                case 1:
                    rb_heal_in_hospital_yes.setChecked(true);
                    break;
                case 0:
                    rb_heal_in_hospital_not.setChecked(true);
                    break;
            }
            et_hospital_num.setText(getMedicalTreatment.getHospital_no());
            et_hospital_time.setText(getMedicalTreatment.getHospital_in_time());
            et_hospital_time2.setText(getMedicalTreatment.getHospital_outs_time());
            String medicint_access2 = getMedicalTreatment.getMedicint_access();
            try{
                medicint_access = Integer.parseInt(medicint_access2);
            }catch (Exception e){
                e.printStackTrace();
            }
            switch (medicint_access2){
                case "1":
                    rb_send_medicine.setChecked(true);
                    break;
                case "2":
                    rb_getmedicine_by_hospital.setChecked(true);
                    break;
                default:
                    rb_other_way.setChecked(true);
                    et_other_way.setText(medicint_access2);
                    break;
            }
            et_heal_sit_checker.setText(getMedicalTreatment.getInvestor());
        }else {
            if (isFirstLoad){
                // et_heal_num.setText("");
            }
            medical_busy_type = 1;
            clearGetMedicalData();
        }
    }

    private void clearGetMedicalData(){
        medical_busy_type = 1;// 新增

//        ll_medicine_heal.setVisibility(View.GONE); // 收缩药物治疗情况
//        iv_medicine_heal.setImageResource(R.mipmap.add);
        // 清空数据
//        et_heal_sit_num.setText("");
        et_eat_medicine_time.setText("");
        cb2_medicine1.setChecked(false);
        cb2_medicine2.setChecked(false);
        cb2_medicine3.setChecked(false);
        cb2_medicine4.setChecked(false);
        cb2_medicine5.setChecked(false);
        cb2_medicine6.setChecked(false);
        cb2_medicine7.setChecked(false);
        cb2_medicine8.setChecked(false);
        cb2_medicine9.setChecked(false);
        cb2_medicine10.setChecked(false);
        cb2_medicine_other.setChecked(false);
        et2_medicine_other.setText("");
        rb_heal_in_hospital_yes.setChecked(true);
        et_hospital_num.setText("");
        et_hospital_time.setText("");
        et_hospital_time2.setText("");
        rb_send_medicine.setChecked(true);
        et_heal_sit_checker.setText("");
        is_hospital = 1; // 是否住院
        medicint_access = 1; // 获取药物方式
    }

    private List<DrugallergyInfoPack> drugallergy_infos = new ArrayList<>(); // 药物安全性
    private List<PremedicalTreatmentPack> premedical_treatments = new ArrayList<>(); // 既往病史
    private List<DiagnosisInfoPack> diagnosis_infos = new ArrayList<>(); // 诊疗数据
    private List<EaProcessPack> ea_processs = new ArrayList<>(); // 疫源处理
    private List<GetMedicalTreatmentPack> meical_treatments = new ArrayList<>(); // 药物治疗情况
    /**
     * 获取人员诊疗全部信息（包括人员基本信息）
     */
    private void GetDiagnosisTreatmentInfo(String id){
        if (isOnLine()){
            // 在线
            RetrofitFactory.getInstance().createApi(RetrofitApi.class).getDiagnosisTreatmentInfo(id,"1")
                    .compose(RxScheduler.rxSchedulerTransform())
                    .subscribe(new BaseObserver<DiagnosisTreatmentInfo>() {

                        @Override
                        protected void onStart() {
                            getLoadingDialog().show();
                        }

                        @Override
                        protected void onSuccess(DiagnosisTreatmentInfo data) throws Exception {
                            LogUtil.d(data.toString());
                            if (data != null){
                                // 设置人员基础数据
                                List<PersonData> person = data.getPerson();
                                showPersonData(person);

                                // 设置药物安全性数据
                                drugallergy_infos = data.getDrugallergy_infos();
                                showDrugallergyData(getMaxdrugallergy_infosNum());

                                // 设置既往病史和治疗情况
                                premedical_treatments = data.getPremedical_treatments();
                                showPremedicalTreatmentInfo(getMaxPreNum());

                                // 设置诊疗数据
                                diagnosis_infos = data.getDiagnosis_infos();
                                showDiagnosisInfo(getMaxdiagnosis_infosNum());

                                // 疫源处理
                                ea_processs = data.getEa_processs();
                                showEaProcessInfo(getMaxea_processsNum());

                                // 药物治疗情况
                                meical_treatments = data.getMeical_treatments();
                                showGetMedicalTreatmentInfo(getMaxmeical_treatmentsNum(),true);
                            }
                        }

                        @Override
                        protected void onFinish() {
                            getLoadingDialog().dismiss();
                        }
                    });
        }else{
            // 离线状态，查询本地数据展示
//            List<PersonData> person = LiteOrmUtil.whereQuery(PersonData.class, "identity_code", id);
            List<PersonData> person = DBManager.get().personDataDao().findById(id);
            showPersonData(person);

//            diagnosis_infos = LiteOrmUtil.whereQuery(DiagnosisInfoPack.class, "identity_code", id);
            diagnosis_infos = DBManager.get().diagnosisInfoPackDao().findById(id);
            showDiagnosisInfo(getMaxdiagnosis_infosNum());

//            ea_processs = LiteOrmUtil.whereQuery(EaProcessPack.class, "identity_code", id);
            ea_processs = DBManager.get().eaProcessPackDao().findById(id);
            showEaProcessInfo(getMaxea_processsNum());

//            meical_treatments = LiteOrmUtil.whereQuery(GetMedicalTreatmentPack.class, "identity_code", id);
            meical_treatments = DBManager.get().getMedicalTreatmentPackDao().findById(id);
                    showGetMedicalTreatmentInfo(getMaxmeical_treatmentsNum(),true);

//            drugallergy_infos = LiteOrmUtil.whereQuery(DrugallergyInfoPack.class, "identity_code", id);
            drugallergy_infos = DBManager.get().drugallergyInfoPackDao().findById(id);
            showDrugallergyData(getMaxdrugallergy_infosNum());

//            premedical_treatments = LiteOrmUtil.whereQuery(PremedicalTreatmentPack.class, "identity_code", id);
            premedical_treatments = DBManager.get().premedicalTreatmentPackDao().findById(id);
            showPremedicalTreatmentInfo(getMaxPreNum());

        }

    }

    /**
     * 保存人员诊疗信息
     * type 3 = 药物治疗   2 = 既往历史    1 = 诊疗
     */
    private void saveDiagnosisTreatment(int type){
        String id_code = et_id_num.getText().toString().trim(); // 身份码
        if (TextUtils.isEmpty(id_code)){
            ToastUtil.showToast(getString(R.string.id_null));
            return;
        }
        String heal_num = et_heal_num.getText().toString().trim(); // 诊疗序号
        String handle_num = et_handle_num.getText().toString().trim(); // 疫源处理序号
        String heal_sit_num = et_heal_sit_num.getText().toString().trim(); // 药物安全性序号

        final DrugallergyInfo drugallergyInfo = new DrugallergyInfo();
        final PremedicalTreatment premedicalTreatment = new PremedicalTreatment();
        final DiagnosisInfo diagnosisInfo = new DiagnosisInfo();
        final EaProcess eaProcess = new EaProcess();
        final GetMedicalTreatment getMedicalTreatment = new GetMedicalTreatment();
        if (type == 3){
            //药物安全性对象
            String record_num = et_medical_num.getText().toString().trim();
            if (TextUtils.isEmpty(record_num)){
                ToastUtil.showToast(getString(R.string.medical_null));
                return;
            }
            drugallergyInfo.setRecord_seq(TextUtils.isEmpty(record_num) ? 0: Integer.parseInt(record_num));
            drugallergyInfo.setAllergy_info(et_allergy_history.getText().toString().trim());
            drugallergyInfo.setAllergy_drug(et_allergy_medicine.getText().toString().trim());
            drugallergyInfo.setIdentity_code(id_code);
            AdverseReaction adverseReaction = new AdverseReaction(); //
            adverseReaction.setAnorexia(cb_eat_feel_down.isChecked() ? 1 : 0);
            adverseReaction.setNausea(cb_nausea.isChecked() ? 1 : 0);
            adverseReaction.setVomiting(cb_vomit.isChecked() ? 1 : 0);
            adverseReaction.setDiarrhea(cb_diarrhea.isChecked() ? 1 : 0);
            adverseReaction.setAbnormalECG(cb_heart_ele_abnormal.isChecked() ? 1 : 0);
            adverseReaction.setHemolysis(cb_hemolysis.isChecked() ? 1 : 0);
            adverseReaction.setOther(cb_medical_other.isChecked() ? et_medical_other.getText().toString().trim() : "");
            drugallergyInfo.setAdverse_reactions(GsonUtil.beanToJson(adverseReaction)); // 不良反应json
            drugallergyInfo.setInvestor(et_medical_investigate.getText().toString().trim());
            drugallergyInfo.setInvest_time(et_medical_investigate_time.getText().toString().trim());
        } else if (type == 2){
            // 既往病史和治疗情况
            String history_num = et_history_num.getText().toString().trim();
            if (TextUtils.isEmpty(history_num)){
                ToastUtil.showToast(getString(R.string.history_num_null));
                return;
            }
            premedicalTreatment.setRecord_seq(TextUtils.isEmpty(history_num) ? 0 : Integer.parseInt(history_num));
            premedicalTreatment.setMalaria_time(et_history_sick_time.getText().toString().trim());
            premedicalTreatment.setIdentity_code(id_code); // id_code
            // 治疗药品
            TokenDrug tokenDrug = new TokenDrug();
            tokenDrug.setArtesunatepiperaquinetablets(cb_medicine1.isChecked() ? 1 : 0);
            tokenDrug.setArtesunatebenflumetoltablets(cb_medicine2.isChecked() ? 1 : 0);
            tokenDrug.setArtesunateamodiquinetablets(cb_medicine3.isChecked() ? 1 : 0);
            tokenDrug.setArtesunatesulfadoxinepyrimethaminetablets(cb_medicine5.isChecked() ? 1 : 0);
            tokenDrug.setDihydroartemisininphosphatepiperaquinetablets(cb_medicine4.isChecked() ? 1 : 0);
            tokenDrug.setArtesunateinjection(cb_medicine6.isChecked() ? 1 : 0);
            tokenDrug.setArtemetherinjection(cb_medicine7.isChecked() ? 1 : 0);
            tokenDrug.setPrimaquine(cb_medicine8.isChecked() ? 1 : 0);
            tokenDrug.setChloroquine(cb_medicine10.isChecked() ? 1 : 0);
            tokenDrug.setSulfadoxinepyrimethaminesp(cb_medicine9.isChecked() ? 1 : 0);
            tokenDrug.setOther(cb_medicine_other.isChecked() ? et_medicine_other.getText().toString().trim() : "");
            premedicalTreatment.setTaken_drags(GsonUtil.beanToJson(tokenDrug)); // 治疗药品json
            premedicalTreatment.setTaken_days(et_medical_days.getText().toString().trim());
            premedicalTreatment.setIs_root_therapy(is_xg + "");
            premedicalTreatment.setInvestor(et_history_investigate.getText().toString().trim());
            premedicalTreatment.setInvest_time(et_history_investigate_time.getText().toString().trim());
        }else if (type == 1){
            if (TextUtils.isEmpty(heal_num)){
                ToastUtil.showToast(getString(R.string.heal_num_null));
                return;
            }
            if (heal_result != 0){
                //诊断正常情况下，报告时间、报告单位、报告人是不必填的，有疑似病例、确诊病例是必须填写
                if(StringUtil.isEmpty(et_sick_upload_time.getText().toString().trim()) ||
                        StringUtil.isEmpty(et_report_unit.getText().toString().trim()) ||
                        StringUtil.isEmpty(et_reporter.getText().toString().trim())){
                    ToastUtil.showToast(ResourceUtil.getString(R.string.report_info_can_not_be_null));
                    return;
                }
            }
            // 诊疗的entity
            diagnosisInfo.setIdentity_code(id_code);
            diagnosisInfo.setRecord_no(TextUtils.isEmpty(heal_num) ? 0 : Integer.parseInt(heal_num));
            diagnosisInfo.setDiagnostics(et_diagnosis_man.getText().toString().trim());
            diagnosisInfo.setDiagnostic_time(et_diagnosis_time.getText().toString().trim());
            diagnosisInfo.setReport_time(et_sick_upload_time.getText().toString().trim());
            diagnosisInfo.setReport_unit(et_report_unit.getText().toString().trim());
            diagnosisInfo.setReportor(et_reporter.getText().toString().trim());
            diagnosisInfo.setDiagnosis_result(heal_result);
            // 症状
            Symptom symptom = new Symptom(); // 症状
            symptom.setNormothermia(cb_temperature_normal.isChecked() ? 1 : 0);
            symptom.setContinuousferer(cb_last_hot.isChecked() ? 1 : 0);
            symptom.setQodferer(cb_other_day_hot.isChecked() ? 1 : 0);
            symptom.setIrregularheatinggap(cb_hot_suddenly.isChecked() ? 1 : 0);
            symptom.setFeeldizzy(cb_cold.isChecked() ? 1 : 0);
            symptom.setSweat(cb_sweat.isChecked() ? 1 : 0);
            symptom.setHeadache(cb_headache.isChecked() ? 1 : 0);
            symptom.setDiarrhea(cb_diarrhea2.isChecked() ? 1 : 0);
            symptom.setMusclesoreness(cb_muscle_ache.isChecked() ? 1 : 0);
            diagnosisInfo.setClinical_symptoms(GsonUtil.beanToJson(symptom));
            // 兼症
            ConSymptom conSymptom = new ConSymptom();
            conSymptom.setShiver(cb_shiver.isChecked() ? 1 : 0);
            conSymptom.setTic(cb_twitch.isChecked() ? 1 : 0);
            conSymptom.setShock(cb_shock.isChecked() ? 1 : 0);
            conSymptom.setOliguria(cb_oliguria.isChecked() ? 1 : 0);
            conSymptom.setHematuria(cb_hematuria.isChecked() ? 1 : 0);
            conSymptom.setOther(cb_other_symptoms.isChecked() ? et_other_symptoms.getText().toString().trim() : "");
            diagnosisInfo.setCon_symptoms(GsonUtil.beanToJson(conSymptom)); // 兼症
            // 发病地点
            diagnosisInfo.setOnset_place("");
            if (!TextUtils.isEmpty(sick_place_county_code)){
                diagnosisInfo.setOnset_place(sick_place_county_code);
            }
            if (!TextUtils.isEmpty(sick_place_village_code)){
                diagnosisInfo.setOnset_place(sick_place_village_code);
            }
            diagnosisInfo.setOnset_place_detail(et_detail_address.getText().toString().trim());
            // 镜检结果
            MicroResults microResults = new MicroResults();
            microResults.setNegative(cb_negative.isChecked() ? 1 : 0);
            microResults.setTertian_malaria(cb_tertian_malaria.isChecked() ? 1 : 0);
            microResults.setFalciparum_malaria(cb_malignant_malaria.isChecked() ? 1 : 0);
            microResults.setQuartan_malaria(cb_quartan_malaria.isChecked() ? 1 : 0);
            microResults.setOvale_malaria(cb_ovale_malaria.isChecked() ? 1 : 0);
            microResults.setMixed_infection(cb_mixed_infection.isChecked() ? 1 : 0);
            diagnosisInfo.setMicro_results(GsonUtil.beanToJson(microResults)); // 镜检结果
            // 原虫密度
            String protozoan_density = et_protozoan_density.getText().toString().trim();
            diagnosisInfo.setProtozoan_density(TextUtils.isEmpty(protozoan_density) ? 0 : Float.parseFloat(protozoan_density));
            // 配子体携带率
            String gametophyte_carrying_rate = et_gametophyte_carrying_rate.getText().toString().trim();
            diagnosisInfo.setGametophyte_carry_rate(TextUtils.isEmpty(gametophyte_carrying_rate) ? 0 : Float.parseFloat(gametophyte_carrying_rate));
            // 雌雄配子体比率
            String ratio_of_male_to_female_gametophyte = et_ratio_of_male_to_female_gametophyte.getText().toString().trim();
            diagnosisInfo.setMtf_gametophyte_rate(TextUtils.isEmpty(ratio_of_male_to_female_gametophyte) ? 0 : Float.parseFloat(ratio_of_male_to_female_gametophyte));
            diagnosisInfo.setMicro_examiner(et_mirror_man.getText().toString().trim());
            diagnosisInfo.setMicro_time(et_mirror_time.getText().toString().trim());
            // rdt镜检结果
            MicroResults rdtResults = new MicroResults();
            rdtResults.setNegative(cb_negative2.isChecked() ? 1 : 0);
            rdtResults.setTertian_malaria(cb_tertian_malaria2.isChecked() ? 1 : 0);
            rdtResults.setFalciparum_malaria(cb_malignant_malaria2.isChecked() ? 1 : 0);
            rdtResults.setQuartan_malaria(cb_quartan_malaria2.isChecked() ? 1 : 0);
            rdtResults.setOvale_malaria(cb_ovale_malaria2.isChecked() ? 1 : 0);
            rdtResults.setMixed_infection(cb_mixed_infection2.isChecked() ? 1 : 0);
            diagnosisInfo.setRdt_result(GsonUtil.beanToJson(rdtResults)); // rdt镜检结果
            diagnosisInfo.setRdt_checker(et_checker.getText().toString().trim());
            diagnosisInfo.setRdt_check_time(et_check_time.getText().toString().trim());
            diagnosisInfo.setIs_roea(is_role);
            diagnosisInfo.setEa_place(""); // 流行区地点
            if (!TextUtils.isEmpty(sick_place_county_code2)){
                diagnosisInfo.setEa_place(sick_place_county_code2);
            }
            if (!TextUtils.isEmpty(sick_place_village_code2)){
                diagnosisInfo.setEa_place(sick_place_village_code2);
            }
            diagnosisInfo.setOea_place(et_detail_address2.getText().toString().trim());
            diagnosisInfo.setIs_transfusion_twoweek(is_transfusion_twoweek);
            diagnosisInfo.setIs_visitor_fever(is_visitor_fever);
            diagnosisInfo.setInfection_source_type(infection_source_type);
            diagnosisInfo.setInvestor(et_checker_source.getText().toString().trim());
            diagnosisInfo.setInvest_time(et_check_time_source.getText().toString().trim());

            // 诊断entity2
            eaProcess.setIdentity_code(id_code);
            eaProcess.setRecord_no(TextUtils.isEmpty(heal_num) ? 0 : Integer.parseInt(heal_num));
            eaProcess.setRecord_seq(TextUtils.isEmpty(handle_num) ? 0 : Integer.parseInt(handle_num));
            eaProcess.setSpray_time(et_ps_time.getText().toString().trim());
            eaProcess.setIndoor_spray_agent(et_inside_ps_thing.getText().toString().trim());
            // 室外喷洒
            OutdoorSprayAgent outdoorSprayAgent = new OutdoorSprayAgent();
            outdoorSprayAgent.setPirimiphos_methyl(rb_jjmdl.isChecked() ? 1 : 0);
            outdoorSprayAgent.setOther(rb_other_ps.isChecked()? et_other_ps.getText().toString().trim() : "");
            eaProcess.setOutdoor_spray_agent(GsonUtil.beanToJson(outdoorSprayAgent));
            eaProcess.setOut_range(out_range);
            eaProcess.setBednets_mu_rate(bednets_mu_rate);
            eaProcess.setInvestor(et_handle_checker.getText().toString().trim());

            // 诊断entity3
            getMedicalTreatment.setIdentity_code(id_code);
            getMedicalTreatment.setRecord_no(TextUtils.isEmpty(heal_num) ? 0 : Integer.parseInt(heal_num));
            getMedicalTreatment.setRecord_seq(TextUtils.isEmpty(heal_sit_num) ? 0 : Integer.parseInt(heal_sit_num));
            getMedicalTreatment.setMedicine_taken_time(et_eat_medicine_time.getText().toString().trim());
            // 治疗药品
            TokenDrug tokenDrug2 = new TokenDrug();
            tokenDrug2.setArtesunatepiperaquinetablets(cb2_medicine1.isChecked() ? 1 : 0);
            tokenDrug2.setArtesunatebenflumetoltablets(cb2_medicine2.isChecked() ? 1 : 0);
            tokenDrug2.setArtesunateamodiquinetablets(cb2_medicine3.isChecked() ? 1 : 0);
            tokenDrug2.setArtesunatesulfadoxinepyrimethaminetablets(cb2_medicine5.isChecked() ? 1 : 0);
            tokenDrug2.setDihydroartemisininphosphatepiperaquinetablets(cb2_medicine4.isChecked() ? 1 : 0);
            tokenDrug2.setArtesunateinjection(cb2_medicine6.isChecked() ? 1 : 0);
            tokenDrug2.setArtemetherinjection(cb2_medicine7.isChecked() ? 1 : 0);
            tokenDrug2.setPrimaquine(cb2_medicine8.isChecked() ? 1 : 0);
            tokenDrug2.setChloroquine(cb2_medicine10.isChecked() ? 1 : 0);
            tokenDrug2.setSulfadoxinepyrimethaminesp(cb2_medicine9.isChecked() ? 1 : 0);
            tokenDrug2.setOther(cb2_medicine_other.isChecked() ? et2_medicine_other.getText().toString().trim() : "");
            getMedicalTreatment.setTaken_drags(GsonUtil.beanToJson(tokenDrug2));
            getMedicalTreatment.setIs_hospital(is_hospital);
            getMedicalTreatment.setHospital_no(et_hospital_num.getText().toString().trim());
            getMedicalTreatment.setHospital_in_time(et_hospital_time.getText().toString().trim());
            getMedicalTreatment.setHospital_outs_time(et_hospital_time2.getText().toString().trim());
            getMedicalTreatment.setMedicint_access(medicint_access == 3 ? et_other_way.getText().toString().trim() : medicint_access + "");
            getMedicalTreatment.setInvestor(et_heal_sit_checker.getText().toString().trim());
            // 服药方式取决于诊断是否得病,得病则属于药物治疗值为1，未得病则属于药物防治值为2，药物防治情况下是否住院等住院信息就不展现
            // getMedicalTreatment.setMedicine_taken_type();
        }

        if (!isOnLine()){
            // 离线状态，保存到本地数据库
            if (type == 1){
                DiagnosisInfoPack diagnosisInfoPack = DiagnosisInfoPack.copyFromNoPack(diagnosisInfo);// 转成pack
                EaProcessPack eaProcessPack = EaProcessPack.copyFromNoPack(eaProcess);// 转成pack
                GetMedicalTreatmentPack getMedicalTreatmentPack = GetMedicalTreatmentPack.copyFromNoPack(getMedicalTreatment);// 转成pack

                diagnosisInfoPack.setOnset_county_name(et_sick_address_county.getText().toString().trim());
                diagnosisInfoPack.setOnset_village_name(et_sick_address_village.getText().toString().trim());
                diagnosisInfoPack.setEa_county_name(et_sick_address_county2.getText().toString().trim());
                diagnosisInfoPack.setEa_village_name(et_sick_address_village2.getText().toString().trim());

                diagnosisInfoPack.setBusy_type(diagnosis_busy_type);
                eaProcessPack.setBusy_type(ea_busy_type);
                getMedicalTreatmentPack.setBusy_type(medical_busy_type);

                if (diagnosis_busy_type == 1){
                    diagnosisInfoPack.setCreateOrUpdate(true);
                    Long insert = DBManager.get().diagnosisInfoPackDao().insert(diagnosisInfoPack);// 保存插入
                    diagnosis_infos.add(diagnosisInfoPack);
                    diagnosis_busy_type = 2;
                }else if (diagnosis_busy_type == 2){
                    diagnosisInfoPack.setCreateOrUpdate(true);
                    long insert = DBManager.get().diagnosisInfoPackDao().update(diagnosisInfoPack);// 保存插入
                    for (int i = 0; i < diagnosis_infos.size(); i++) {
                        DiagnosisInfoPack diagnosisInfoPack1 = diagnosis_infos.get(i);
                        if (diagnosisInfoPack1.getRecord_no() == diagnosisInfoPack.getRecord_no()){
                            diagnosis_infos.set(i,diagnosisInfoPack);
                            break;
                        }
                    }
                }

                if (!TextUtils.isEmpty(handle_num)){
                    if (ea_busy_type == 1){
                        eaProcessPack.setCreateOrUpdate(true);
                        Long insert1 = DBManager.get().eaProcessPackDao().insert(eaProcessPack);// 保存插入
                        ea_processs.add(eaProcessPack);
                        ea_busy_type = 2;
                    }else if (ea_busy_type == 2){
                        eaProcessPack.setCreateOrUpdate(true);
                        Long insert1 = DBManager.get().eaProcessPackDao().insert(eaProcessPack);// 保存插入
                        for (int i = 0; i < ea_processs.size(); i++) {
                            EaProcessPack eaProcessPack1 = ea_processs.get(i);
                            if (eaProcessPack1.getRecord_seq() == eaProcessPack.getRecord_seq()){
                                ea_processs.set(i,eaProcessPack);
                                break;
                            }
                        }
                    }
                }

                if (!TextUtils.isEmpty(heal_sit_num)){
                    if (medical_busy_type == 1){
                        getMedicalTreatmentPack.setCreateOrUpdate(true);
                        Long insert2 = DBManager.get().getMedicalTreatmentPackDao().insert(getMedicalTreatmentPack);// 保存插入
                        meical_treatments.add(getMedicalTreatmentPack);
                        medical_busy_type = 2;
                    }else if (medical_busy_type == 2){
                        getMedicalTreatmentPack.setCreateOrUpdate(true);
                        int insert2 = DBManager.get().getMedicalTreatmentPackDao().update(getMedicalTreatmentPack);// 保存插入
                        for (int i = 0; i < meical_treatments.size(); i++) {
                            GetMedicalTreatmentPack getMedicalTreatmentPack1 = meical_treatments.get(i);
                            if (getMedicalTreatmentPack1.getRecord_seq() == getMedicalTreatmentPack.getRecord_seq()){
                                meical_treatments.set(i,getMedicalTreatmentPack);
                                break;
                            }
                        }
                    }
                }

                ToastUtil.showToast(getString(R.string.save_success));

            }else if (type == 2){
                PremedicalTreatmentPack premedicalTreatmentPack = PremedicalTreatmentPack.copyFromNoPack(premedicalTreatment);
                premedicalTreatmentPack.setBusy_type(premedical_busy_type);
                if (premedical_busy_type == 1){
                    premedicalTreatmentPack.setCreateOrUpdate(true);
                    Long insert = DBManager.get().premedicalTreatmentPackDao().insert(premedicalTreatmentPack); // 保存插入
                    if (insert > 0){
                        ToastUtil.showToast(getString(R.string.save_success));
                        premedical_treatments.add(premedicalTreatmentPack);
                        premedical_busy_type = 2;
                    }
                }else if (premedical_busy_type == 2){
                    premedicalTreatmentPack.setCreateOrUpdate(true);
                    Long insert = DBManager.get().premedicalTreatmentPackDao().insert(premedicalTreatmentPack); // 保存插入
                    if (insert > 0){
                        ToastUtil.showToast(getString(R.string.save_success));
                        for (int i = 0; i < premedical_treatments.size(); i++) {
                            PremedicalTreatmentPack premedicalTreatmentPack1 = premedical_treatments.get(i);
                            if (premedicalTreatmentPack1.getRecord_seq() == premedicalTreatmentPack.getRecord_seq()){
                                premedical_treatments.set(i,premedicalTreatmentPack);
                                break;
                            }
                        }
                    }
                }

            }else if (type == 3){
                DrugallergyInfoPack drugallergyInfoPack = DrugallergyInfoPack.copyFromNoPack(drugallergyInfo);
                drugallergyInfoPack.setBusy_type(drugallergy_busy_type);
                if (drugallergy_busy_type == 1){
                    drugallergyInfoPack.setCreateOrUpdate(true);
                    Long insert = DBManager.get().drugallergyInfoPackDao().insert(drugallergyInfoPack);// 保存插入
                    if (insert > 0){
                        ToastUtil.showToast(getString(R.string.save_success));
                        drugallergy_infos.add(drugallergyInfoPack);
                        drugallergy_busy_type = 2;
                    }
                }else if (drugallergy_busy_type == 2){
                    drugallergyInfoPack.setCreateOrUpdate(true);
                    int insert = DBManager.get().drugallergyInfoPackDao().update(drugallergyInfoPack);// 保存插入
                    if (insert > 0){
                        ToastUtil.showToast(getString(R.string.save_success));
                        for (int i = 0; i < drugallergy_infos.size(); i++) {
                            DrugallergyInfoPack drugallergyInfoPack1 = drugallergy_infos.get(i);
                            if (drugallergyInfoPack1.getRecord_seq() == drugallergyInfoPack.getRecord_seq()){
                                drugallergy_infos.set(i,drugallergyInfoPack);
                                break;
                            }
                        }
                    }
                }
            }
        }else {
            // 在线状态
            DataCollectRequest dataCollectRequest = new DataCollectRequest();
            DataCollectType dataCollectType = new DataCollectType();
            if (type == 1){
                // 诊疗信息数据
                List<DiagnosisInfo> diagnosis_infos = new ArrayList<>();
                diagnosis_infos.add(diagnosisInfo);
                dataCollectRequest.setDiagnosis_infos(diagnosis_infos);
                dataCollectType.setDiagnosis_infos(diagnosis_busy_type);

                // 疫源处理序号不为空才添加
                if (!TextUtils.isEmpty(handle_num)){
                    List<EaProcess> ea_processs = new ArrayList<>();
                    ea_processs.add(eaProcess);
                    dataCollectRequest.setEa_processs(ea_processs);
                    dataCollectType.setEa_processs(ea_busy_type);
                }
                // 药物治疗情况序号不为空才添加
                if (!TextUtils.isEmpty(heal_sit_num)){
                    List<GetMedicalTreatment> meical_treatments = new ArrayList<>();
                    meical_treatments.add(getMedicalTreatment);
                    dataCollectRequest.setMeical_treatments(meical_treatments);
                    dataCollectType.setMeical_treatments(medical_busy_type);
                }

            }else if (type == 2){ // 既往病史
                List<PremedicalTreatment> premedical_treatments = new ArrayList<>();
                premedical_treatments.add(premedicalTreatment);
                dataCollectRequest.setPremedical_treatments(premedical_treatments);
                dataCollectType.setPremedical_treatments(premedical_busy_type);
            }else if (type == 3){ // 药物安全性
                List<DrugallergyInfo> drugallergy_infos = new ArrayList<>();
                drugallergy_infos.add(drugallergyInfo);
                dataCollectRequest.setDrugallergy_infos(drugallergy_infos);
                dataCollectType.setDrugallergy_infos(drugallergy_busy_type);
            }

            String json = GsonUtil.beanToJson(dataCollectRequest);
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);

            RetrofitFactory.getInstance().createApi(RetrofitApi.class).saveDiagnosisTreatment(GsonUtil.beanToJson(dataCollectType),requestBody)
                    .compose(RxScheduler.rxSchedulerTransform())
                    .subscribe(new BaseObserver<Object>() {

                        @Override
                        protected void onStart() {
                            getLoadingDialog().show();
                        }

                        @Override
                        protected void onSuccess(Object data) throws Exception {
                            ToastUtil.showToast(getString(R.string.save_success));
                            if (type == 1){
                                DiagnosisInfoPack diagnosisInfoPack = DiagnosisInfoPack.copyFromNoPack(diagnosisInfo);
                                boolean isExistDiagnosisInfo = false;
                                for (int i = 0; i < diagnosis_infos.size(); i++) {
                                    DiagnosisInfoPack diagnosisInfoPack1 = diagnosis_infos.get(i);
                                    if (diagnosisInfoPack1.getRecord_no() == diagnosisInfoPack.getRecord_no()){
                                        diagnosis_infos.set(i,diagnosisInfoPack);
                                        isExistDiagnosisInfo = true;
                                        break;
                                    }
                                }
                                if (!isExistDiagnosisInfo){
                                    diagnosis_infos.add(diagnosisInfoPack);
                                }

                                diagnosis_busy_type = 2;
                                if (!TextUtils.isEmpty(handle_num)){
                                    EaProcessPack eaProcessPack = EaProcessPack.copyFromNoPack(eaProcess);
                                    boolean isExisteaProcess = false;
                                    for (int i = 0; i < ea_processs.size(); i++) {
                                        EaProcessPack eaProcessPack1 = ea_processs.get(i);
                                        if (eaProcessPack1.getRecord_seq() == eaProcessPack.getRecord_seq()){
                                            ea_processs.set(i,eaProcessPack);
                                            isExisteaProcess = true;
                                            break;
                                        }
                                    }
                                    if (!isExisteaProcess){
                                        ea_processs.add(eaProcessPack);
                                    }
                                    ea_busy_type = 2;
                                }
                                if (!TextUtils.isEmpty(heal_sit_num)){
                                    GetMedicalTreatmentPack getMedicalTreatmentPack = GetMedicalTreatmentPack.copyFromNoPack(getMedicalTreatment);

                                    boolean isExistGetMedicalTreatment = false;
                                    for (int i = 0; i < meical_treatments.size(); i++) {
                                        GetMedicalTreatmentPack getMedicalTreatmentPack1 = meical_treatments.get(i);
                                        if (getMedicalTreatmentPack1.getRecord_seq() == getMedicalTreatmentPack.getRecord_seq()){
                                            meical_treatments.set(i,getMedicalTreatmentPack);
                                            isExistGetMedicalTreatment = true;
                                            break;
                                        }
                                    }
                                    if (!isExistGetMedicalTreatment){
                                        meical_treatments.add(getMedicalTreatmentPack);
                                    }

                                    medical_busy_type = 2;
                                }
                            }else if (type == 2){
                                PremedicalTreatmentPack premedicalTreatmentPack = PremedicalTreatmentPack.copyFromNoPack(premedicalTreatment);

                                boolean isExistGetMedicalTreatment = false;
                                for (int i = 0; i < premedical_treatments.size(); i++) {
                                    PremedicalTreatmentPack premedicalTreatmentPack1 = premedical_treatments.get(i);
                                    if (premedicalTreatmentPack1.getRecord_seq() == premedicalTreatmentPack.getRecord_seq()){
                                        premedical_treatments.set(i,premedicalTreatmentPack);
                                        isExistGetMedicalTreatment = true;
                                        break;
                                    }
                                }
                                if (!isExistGetMedicalTreatment){
                                    premedical_treatments.add(premedicalTreatmentPack);
                                }

                                premedical_busy_type = 2;
                            }else if (type == 3){
                                DrugallergyInfoPack drugallergyInfoPack = DrugallergyInfoPack.copyFromNoPack(drugallergyInfo);

                                boolean isExistDrugallergyInfo = false;
                                for (int i = 0; i < drugallergy_infos.size(); i++) {
                                    DrugallergyInfoPack drugallergyInfoPack1 = drugallergy_infos.get(i);
                                    if (drugallergyInfoPack1.getRecord_seq() == drugallergyInfoPack.getRecord_seq()){
                                        drugallergy_infos.set(i,drugallergyInfoPack);
                                        isExistDrugallergyInfo = true;
                                        break;
                                    }
                                }
                                if (!isExistDrugallergyInfo){
                                    drugallergy_infos.add(drugallergyInfoPack);
                                }

                                drugallergy_busy_type = 2;
                            }
                        }

                        @Override
                        protected void onFinish() {
                            getLoadingDialog().dismiss();
                        }
                    });

        }

    }

}
